问题
I want to apply search on multiple fields in sapui5 on 'Name' and 'Phone No' fields. I am able to apply single field search on 'Name' field but not on 'Phone No' field. When trying to implement on 'Phone No.' field, it doesn't work. Also, it doesn't show any errors. Please see the code below and help.
Contacts.controller.js :
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel",
"sap/ui/model/Filter",
"sap/ui/model/FilterOperator"
], function(Controller,JSONModel,Filter,FilterOperator) {
"use strict";
return Controller.extend("ContactsList.controller.Contacts", {
onFilterContacts : function (oEvent) {
var aFilter = [];
var aFilter1 =[];
var tFilter=[];
var sQuery = oEvent.getParameter("query");
aFilter.push(new Filter("Name", FilterOperator.Contains, sQuery));
aFilter1.push(new Filter("Phone No.", FilterOperator.Contains, sQuery));
tFilter = new Filter([aFilter,aFilter1],false);
// filter binding
var oList = this.byId("contactList");
var oBinding = oList.getBinding("items");
oBinding.filter(tFilter);
}
});
});
Contacts.view.xml :
<mvc:View controllerName="ContactsList.controller.Contacts" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m"
xmlns:html="http://www.w3.org/1999/xhtml">
<List id="contactList" class="sapUiLargeMarginLeft sapUiLargeMarginRight" width="auto" items="{contact>/ContactList}">
<headerToolbar>
<Toolbar>
<ToolbarSpacer/>
<SearchField width="50%" search="onFilterContacts"/>
</Toolbar>
</headerToolbar>
<items>
<ObjectListItem title="{contact>Name}" number="{contact>Phone No.}"></ObjectListItem>
</items>
</List>
</mvc:View>
ContactList.json :
{
"ContactList": [
{
"Name": "Swapnil Garg",
"Phone No.": 1234
},
{
"Name": "Ashutosh Garg",
"Phone No.": 5678
},
{
"Name": "Rajat Sharma",
"Phone No.": 1987
},
{
"Name": "Ankur Shukla",
"Phone No.": 6789
},
{
"Name": "Naman Kumar",
"Phone No.": 2345
}
]
}
回答1:
Only String
values are supported for sap.ui.model.FilterOperator.Contains
. That's the reason why you get an error. Since you don't need to calculate with the phone numbers, why don't you try something like this? I cleaned up the Controller code and added quotes to the phone numbers in your json file.
Controller
onFilterContacts: function(oEvent) {
var sQuery = oEvent.getParameter("query");
var oFilter1 = new Filter("Name", FilterOperator.Contains, sQuery);
var oFilter2 = new Filter("Phone No.", FilterOperator.Contains, sQuery);
var arrFilter = new Filter([oFilter1, oFilter2], false);
// filter binding
var oList = this.byId("contactList");
var oBinding = oList.getBinding("items");
oBinding.filter(arrFilter);
}
ContactList.json
{
"ContactList": [
{
"Name": "Swapnil Garg",
"Phone No.": "1234"
},
{
"Name": "Ashutosh Garg",
"Phone No.": "5678"
},
{
"Name": "Rajat Sharma",
"Phone No.": "0987"
},
{
"Name": "Ankur Shukla",
"Phone No.": "1342"
},
{
"Name": "Naman Kumar",
"Phone No.": "1928"
}
]
}
来源:https://stackoverflow.com/questions/51837505/unable-to-apply-search-on-multiple-fields-in-sapui5