cancel
Showing results for 
Search instead for 
Did you mean: 

Table Filter for Odata Model

gopi_nidjelli2
Participant
0 Kudos

Hi All,

      I have a table which has binding to a Odata Model. I have a search field at the top of the table which should filter my table values.

var sField = "profam";

     var sQuery = oEvent.getParameter("query");

  

  var filters = new sap.ui.model.Filter(sField, sap.ui.model.FilterOperator.Contains, sQuery);

   

     // update list binding

var list = this.getView().byId("table");

var binding = list.getBinding("items");

binding.filter(filters);

But the table is not filtering with the search criteria. 

Does Client Side Filter works for Odata model or we need to filter in the Odata ?

Thanks

Gopi

Accepted Solutions (1)

Accepted Solutions (1)

saivellanki
Active Contributor

Hi Gopi,

OData Model is a Server-Side Model and it supports server-side filtering.

But from 'v2' version of OData Model, you have an option to set the mode to 'Client' / 'Server' / 'Auto' depending on your use case.

You have to set the mode, when you're binding items to table, for example:


oTable.bindItems({

     path: 'yourBindingPath',

     parameters: {

          mode: sap.ui.model.odata.OperationMode.Client

     }

});

API: JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.odata.OperationMo...


Regarding your issue: Since you didn't set any mode, it automatically picks 'Server' as the mode and does the filtering on server side. Your code looks fine. But, can you check the developer console and see if there are any errors?


Regards,

Sai.

gopi_nidjelli2
Participant
0 Kudos

Hi Sai,

I tried as below. The table gets refreshed but data is not filtered.

  var sQuery = oEvent.getParameter("query");
var sField = "Lbiprofam";
var oFilters = new sap.ui.model.Filter(sField, sap.ui.model.FilterOperator.Contains, sQuery);

var oTable = this.getView().byId("table");     
oTable.bindItems("/ZPRODFAM_HCISet",ItemTemplate,  sap.ui.model.odata.OperationMode.Client,oFilters );

Am I missing anything here?

Thanks Gopi

saivellanki
Active Contributor
0 Kudos

Gopi,

You have to set the mode at first when you bind items to table for the first time. No need to do it again when you filter the table.

Your below code is good (Actual Question Code), just set a break point and see where it is going wrong.



var sField = "profam";

var sQuery = oEvent.getParameter("query");

var filters = new sap.ui.model.Filter(sField, sap.ui.model.FilterOperator.Contains, sQuery);

// update list binding

var list = this.getView().byId("table");

var binding = list.getBinding("items");

binding.filter(filters);

Regards,

Sai.

gopi_nidjelli2
Participant
0 Kudos

Hi Sai,

        The First time data is fetched from Odata model. Based on the dropdown field I selected I want to filter the data by calling oModel.read(filters) or filter at client side.

If I set as Client side will the other code work? or do I need to set it to Auto?

Thanks

Gopi

Answers (2)

Answers (2)

venkatachala_ck
Active Participant
0 Kudos

Hi Gopi,

You can achieve this in  many ways,

1) If your  filtering the  data within  the Collection then its better to store data in json Model and as times you want.

ex:

oTable = this.getView().byId("tableid");

var odata Model = new sap.ui.model.odata.ODataModel("/XXX/YYYY");

oModel.read("/UrCollection", null, null, true, function(oData, oResponse) {

    jsonModel = new sap.ui.model.json.JSONModel({oData.results });

    

// bindmodel to controller

oTable.setModel(jsonModel);


}, function(oData, oResponse) { });    



//onClick of Search write this below code


var searchString = this.getView().byId("idSrchField").getValue();

  var filters = new sap.ui.model.Filter("getBusinessAreaData", sap.ui.model.FilterOperator.Contains, searchString);

  // update list binding

  this.getView().byId("tableid").getBinding("items").filter(filters);

2)   writing filter Query option in url itself.

use this method in case if you want updated data each and every time.

var odataModel = new sap.ui.model.odata.ODataModel("/XXX/YYYY");

odataModel .read("/UrCollection?$filter=Address/City eq 'Redmond'", null, null, true, function(oData, oResponse) {

 

  jsonModel = new sap.ui.model.json.JSONModel({oData.results });


// bindmodel to controller

oTable.setModel(jsonModel );



}, function(oData, oResponse) { });

For more information please refer below link.

URI Conventions (OData Version 2.0) · OData - the Best Way to REST

Thanks

Venkat

Former Member
0 Kudos

Hi,

Client Side Filter works irrespective of the data model, as it's filtering on the binding.


Sample code, which is working fine for me


  var filters = [];

  var query = evt.getParameter("query");

 

  var filter = new sap.ui.model.Filter("SupplierName", sap.ui.model.FilterOperator.Contains, query);

  filters.push(filter);

 

  // update list binding

  var list = sap.ui.getCore().byId("listid");

  var binding = list.getBinding("items");

  binding.filter(filters);

Thanks,

Namita