cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple filter for searchField

Former Member
0 Kudos

Hello,

We are trying to implement multiple filters for searchField as below:

     var filters = [];

     var query = this.getView().byId("searchField").getValue();

     if(query && query.length > 0)

     {

          filters = [new sap.ui.model.Filter("Firstname", sap.ui.model.FilterOperator.EQ, query),

                         new sap.ui.model.Filter("Lastname", sap.ui.model.FilterOpeator.EQ, query)];

     }

     this.getView().byId("list").getBinding("items").filter(new sap.ui.model.Filter(filters, false));

The single filter is working.

Seeking help.

Regards,

Ajay

Accepted Solutions (0)

Answers (3)

Answers (3)

karthikarjun
Active Contributor
0 Kudos

Hi Ajay,

Filter is the property which can associated with n number of filter condition.

For ex

    filters = [new sap.ui.model.Filter("Firstname", sap.ui.model.FilterOperator.EQ, query),

                         new sap.ui.model.Filter("Lastname", sap.ui.model.FilterOpeator.EQ, query)];

     }

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

Use this...

First it will check all the records inside this filter array. And it will return the appropriate results to you.

If you want or operator, you should create one radio button to specify the filter,

For ex,

1. FilterbyFirstName 2.Filterbysecondname

According to the selection add filter to the array.

Thanks,

Karthik A

Thanks,

Karthik A

Former Member
0 Kudos

Thanks Sai and Karthik.

I am having the samewhat code for filter.

Can you please tell me about the necessary code in abap service class for the same.

Regards,

Ajay

saivellanki
Active Contributor
0 Kudos

Hi Ajay,


You wanted to do filtering on server side / client side?

Also are you using JSON / OData model?

Regards,

Sai Vellanki.

Former Member
0 Kudos

Hi Sai,

I need filters on client side only.

But I as per my understanding, same filter logic should be there at back end too. Right?

I am using OData model.

When I do filter with OR condition, i.e pushing two filters as below:

     http://<server -ip>:<port>/<------path----->/ServiceUrl/EntitySet?$filter=FirstName eq 'Abc' or           LastName eq 'Abc'

Then, when we debug at back end, this OR condition is not at all hitting the filter_options condition where the filter logic is present for the both properties.

Can you please tell me any other changes at back end for this OR condition to work.

Regards,

Ajay

Former Member
0 Kudos

Hi Ajay,

http://<server -ip>:<port>/<------path----->/ServiceUrl/EntitySet?$filter=(FirstName eq 'Abc'  or  LastName eq 'Abc')

You are doing serverside filtering here.If you need only client side filtering.Get data from odata and put in json model.Odata model is server side model and json model is client side model.

Refer these threads for client side filtering

Regards

karthikarjun
Active Contributor
saivellanki
Active Contributor
0 Kudos

Will this help? JS Bin - Collaborative JavaScript Debugging

You can search using LastName / FirstName.

Regards,

Sai Vellanki.

saivellanki
Active Contributor
0 Kudos

Hi Ajay,

If you want both filters to work, then try this -


this.getView().byId("list").getBinding("items").filter(new sap.ui.model.Filter(filters, true));

If you use false, the 'or' conjunction will be applied. If it is true then 'and' conjunction will be applied.

Check the API for more info: JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.Filter


Regards,

Sai Vellanki.

Former Member
0 Kudos

Thanks Sai for the prompt reply.

But, I need the OR operator for this search.

Either by First name or by last name.

Please help.

saivellanki
Active Contributor
0 Kudos

Ajay,

Got it, try like this -


var filters = [];

var query = this.getView().byId("searchField").getValue();

if(query && query.length > 0)

     {

      var oFilter1 = [new sap.ui.model.Filter("Firstname", sap.ui.model.FilterOperator.EQ, query),new sap.ui.model.Filter("Lastname", sap.ui.model.FilterOperator.EQ, query)];

   var allFilters = new sap.ui.model.Filter(oFilter1, false);

   filters.push(allFilters);

     }

this.getView().byId("list").getBinding("items").filter(filters, "Application");

Regards,

Sai Vellanki.