无法对sapui5中的多个字段应用搜索

时间:2018-08-14 08:56:21

标签: javascript json sapui5

我想对sapui5中“名称”和“电话号码”字段上的多个字段应用搜索。我可以在“名称”字段上应用单字段搜索,但不能在“电话号码”字段上应用单字段搜索。尝试在“电话号码”上实施时字段,它不起作用。而且,它不显示任何错误。请参阅下面的代码和帮助。

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 个答案:

答案 0 :(得分:1)

String仅支持sap.ui.model.FilterOperator.Contains值。这就是为什么您会出错的原因。由于您不需要计算电话号码,为什么不尝试这样的事情呢?我清理了Controller代码,并在json文件中的电话号码中加上了引号。

控制器

    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"
    }
  ]
}