I alredy have an sap.m.list which is already filtered based on one property.Now I again need to apply filter for that list to implement live search.
filterList:function(evt){
debugger;
var filters = [];
var oFilter="";
var query = evt.oSource.mProperties.value;
if (query && query.length > 0) {
var filter = new sap.ui.model.Filter("CLTYPE", sap.ui.model.FilterOperator.Contains, query);
filters.push(filter);
var filter1 = new sap.ui.model.Filter("CLCAT", sap.ui.model.FilterOperator.Contains, query);
filters.push(filter1);
var filter2 = new sap.ui.model.Filter("CLNUM", sap.ui.model.FilterOperator.Contains, query);
filters.push(filter2);
oFilter = new sap.ui.model.Filter( filters, false );
}
// update list binding
var list = sap.ui.getCore().byId("List");
var binding = list.getBinding("items");
binding.filter(oFilter);
},
here my list is already filtered.here list.getBinding('items') will give me all the items not the filtered Items.I applied filter for my sap.m.list as follows
obj[filterParam] = context;
var contextClauses=_.where(clauses,obj);
sap.ui.getCore().getModel('ClauseModel').setProperty("/DATA/CURRENTCLAUSES",contextClauses);
// update list binding
var list = sap.ui.getCore().byId("List");
var binding = list.getBinding("items");
binding.filter(oFilter);
how to implement that??any suggestions?
Add livechange
to your textfield
and call a function to filter your list.
new sap.ui.commons.TextField({type:"Text",
value:"",
liveChange: function(oEvent){
//Now call a function to filter your list.
filterList(oEvent,oStorage.get("previousFilteredList"));
}});
filterList: function(oEvent,yourObj){
var like = oEvent.getParameter("liveValue");
var oFilter = new sap.ui.model.Filter("name",
sap.ui.model.FilterOperator.StartsWith,like);
var element = sap.ui.getCore().getElementById("sample");
var listBinding = element.getBinding("items");
listBinding.filter([oFilter]);
//Add the filtered object oStorage after every filter to access the previous filtered data
//oStorage.put("previousFilteredList","yourobj");
},
//Wherever you filter your data first.
oStorage = jQuery.sap.storage("session");
oStorage.put("previousFilteredList","yourobj");
I think you have 2 options:
- Apply the original filter and then the additional filters.
Make a new instance of the initially filtered list:
// assumes you have declared lists (initialList and newList)
// values between <..> should be replaced
jQuery.each (initialList, function (index, item) {
if (item.<someField> == <somevalue>) {
// you could add an additional field to the new list item like so:
item["<newFieldName"] = <newValue>;
if (item.<someOtherField> >= <someOtherValue>) {
newList.push(item); // adds this item to the new array / list
}
}
newListModel.setData(newList); // assumes newListModel has been created
//bind to control
I solved it using the 'and' condition in filter
var mainFilter=new sap.ui.model.Filter(parameter,sap.ui.model.FilterOperator.Contains,context);
var query =evt.getParameter("newValue");
if (query && query.length > 0)var filter1= new sap.ui.model.Filter("CLNAME",sap.ui.model.FilterOperator.Contains,query);
filtersArr1.push(filter1);
filtersArr1.push(mainFilter);
var filter2 = new sap.ui.model.Filter("CLNUM",sap.ui.model.FilterOperator.Contains, query);
filtersArr2.push(filter2);
filtersArr2.push(mainFilter);oFilter1 = new sap.ui.model.Filter(filtersArr1,true);oFilter2 = new sap.ui.model.Filter(filtersArr2,true);filterGroup.push(oFilter1,oFilter2);oFilter4 = new sap.ui.model.Filter(filterGroup,false);
binding.filter(oFilter4);
WORKING FINE
var listFilter=new sap.ui.model.Filter(parameter,sap.ui.model.FilterOperator.Contains,context);
var query =evt.getParameter("newValue");
if (query && query.length > 0)
var filter1= new sap.ui.model.Filter({
new sap.ui.model.Filter("CLNAME",sap.ui.model.FilterOperator.Contains,query),
new sap.ui.model.Filter("CLNUM",sap.ui.model.FilterOperator.Contains, query)}, and:false),
var oFilter = new sap.ui.model.Filter( [filter1, listFilter], true)
binding.filter(oFilter);
});