Skip to Content
0
Former Member
May 08, 2014 at 07:39 PM

Filter Across Multiple Columns on JSONModel

619 Views

I'm working of an example I found and tweaked which will search and sort a list. Have a list control with a searchField control that will filter the list based on one column (last name). I would like to use the same searchField control to search/filter against the first name and department columns as well.

VIEW

// USING A LOCAL JSONMODEL
var oList = new sap.m.List( "searchList", {
showUnread: true,
mode: jQuery.device.is.phone ? sap.m.ListMode.None : sap.m.ListMode.SingleSelectMaster,
select: [oController.onListSelect, oController]
});
oList.setModel(oModel);

var oSearcher = new sap.m.SearchField( "liveSearch", {
enabled: true,
visible: true,
liveChange: [oController.onSearch, oController]
});

var oLastSorter = new sap.ui.model.Sorter("EMP_LAST_NAME", false, function (oContext) {
var name = oContext.getProperty("EMP_LAST_NAME");
var sKey = name.charAt(0);
var text = name.charAt(0);
return {
key: sKey, // group by first letter of last name
text: text
};
});

var oItemTemplate = new sap.m.StandardListItem({
title: {
parts: [{path: "EMP_FIRST_NAME"},
{path: "EMP_LAST_NAME"}],
formatter : function(prop1, prop2) {
var calcValue = prop1 + " " + prop2;
return calcValue;
},
},
description: "{POS_TITLE}",
type: jQuery.device.is.phone? sap.m.ListType.Active : sap.m.ListType.None,
icon: {
path: "image",
formatter: function (sIcon) {
return sIcon;
}
},
press: [oController.onListItemTap, oController]
}); oList.bindAggregation("items", {
path: "/contents",
template: oItemTemplate,
sorter: oLastSorter
});

CONTROLLER

onSearch : function (oEvent) {
var aFilters = [];
var sQuery = oEvent.getSource().getValue();
if (sQuery && sQuery.length > 0) {
var filter = new sap.ui.model.Filter("EMP_LAST_NAME", sap.ui.model.FilterOperator.Contains, sQuery);
aFilters.push(filter);
} // UPDATE LIST BINDING
console.log(oList);
var list = sap.ui.getCore().getElementById('searchList');
var binding = list.getBinding("items");
binding.filter(aFilters, "Application");
},

I thought I could simple add new filters like below:

 var filter2 = new sap.ui.model.Filter("EMP_FIRST_NAME", sap.ui.model.FilterOperator.Contains, sQuery);
 var filter3 = new sap.ui.model.Filter("DEPARTMENT", sap.ui.model.FilterOperator.Contains, sQuery);

But that doesn't seem to work. Any ideas?