gpt4 book ai didi

javascript - 无法在 sapui5 中对多个字段应用搜索

转载 作者:行者123 更新时间:2023-11-30 20:16:56 25 4
gpt4 key购买 nike

我想在 sapui5 中的“姓名”和“电话号码”字段上对多个字段应用搜索。我可以在“姓名”字段上应用单字段搜索,但不能在“电话号码”字段上应用。尝试在“电话号码”上实现时领域,它不起作用。此外,它没有显示任何错误。请参阅下面的代码并提供帮助。

联系人.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);
}

});

});

联系人.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>

联系人列表.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 文件中为电话号码添加了引号。

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

关于javascript - 无法在 sapui5 中对多个字段应用搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51837505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com