无法对sapui5中的多个字段应用搜索 [英] unable to apply search on multiple fields in sapui5

查看:89
本文介绍了无法对sapui5中的多个字段应用搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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:

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

推荐答案

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

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.

控制器

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

这篇关于无法对sapui5中的多个字段应用搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆