如何在SAPUI5中输入有独特的建议

时间:2016-09-08 12:44:28

标签: javascript sapui5

我正在尝试从输入框中获取建议,但是如果模型在wheelName中有多个值,如“wheel1”,“wheel1”,“wheel2”,并且有了这个,当我在输入框中输入“wheel1”时,我得到2建议为wheel1,wheel1,但我想要一个独特的建议,即wheel1只能显示一次。

输入声明如下所示: -

<Input
            id="wheelInput"
            type="Text"
            placeholder="Enter Wheel..."
            showSuggestion="true"
            maxLength="40"
            startSuggestion="3"
            suggestionItems="{wheel>/results}" >
            <suggestionItems>
                <core:Item text="{wheel>wheelName}"/>
            </suggestionItems>
        </Input>

1 个答案:

答案 0 :(得分:1)

假设您的结果列表与您在输入中键入的每个字符不同,您可以将函数附加到Input字段的liveChange。

然后,您可以将自定义逻辑(例如,没有双重名称)放入单独的模型属性中。我还没有对代码进行测试,但我应该工作(如果我没有写错字)。

查看:

<Input
            id="wheelInput"
            type="Text"
            placeholder="Enter Wheel..."
            showSuggestion="true"
            maxLength="40"
            liveChange="filterWheelList"
            startSuggestion="3"
            suggestionItems="{wheel>/filteredWheelList}" >
            <suggestionItems>
                <core:Item text="{wheel>wheelName}"/>
            </suggestionItems>
        </Input>

控制器:

   filterWheelList: function(){
    var wheelModel = sap.ui.getCore().getModel("wheelModel");
    var wheelList = wheelModel.getProperty("/results");
    var uniqueNames = [];

    var filteredWheelList = wheelList.filter(function(wheel){
    if (uniqueNames.indexOf(wheel.wheelName) === -1){
    uniqueNames.push(wheel.wheelName);
    return true;
    } else {
    return false;
    }
    });

    wheelModel.setProperty("/filteredWheelList", filteredWheelList);

    }