SAPUI5:自定义过滤包含来自控制器的动态绑定

时间:2019-12-10 13:37:51

标签: sapui5

我想使用包含以下内容的自定义过滤:https://sapui5.hana.ondemand.com/#/entity/sap.m.ComboBox/sample/sap.m.sample.ComboBoxFilteringContains
 但问题是oLocation.setFilterFunction不是函数:(
 项没有定义到组合框中:

<ComboBox id="my-id"  selectionChange='onChange'>
                <core:Item key="{key}" text="{text}" />
</ComboBox>

因为它们是在控制器中定义的:

oLocation.bindItems({
                    path: "backEnd>/Prod(Id1='" + Subid+ "',cat='" + vBpId +
                        "',ApplicationKey='"/ProductSet",
                    filters: [
                        Filter
                    ],
                    template: new Item({
                        key: '{backEnd>Id}',
                        text: '{backEnd>Description}',
                        customData: [{
                            Type: "sap.ui.core.CustomData",
                            key: "Other",
                            value: '{backEnd>Other}'
                        }]
                    }),
                    /*etc.*/
}).setFilterFunction(function(sTerm, oItem) {
                return oItem.getText().match(new RegExp(sTerm, "i"))
            });

有人可以解决吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:
我创建了一个控件ComboBoxContain,它重写了ComboBox filterItems,如下所示:

ComboBox.prototype.filterItems = function(mOptions, aItems) {
            var sProperty = mOptions.property,
                sValue = mOptions.value,
                bEmptyValue = sValue === "",
                bMatch = false,
                sMutator = "get" + sProperty.charAt(0).toUpperCase() + sProperty.slice(1),
                aFilteredItems = [],
                oItem = null;

            aItems = aItems || this.getItems();
            if (!bEmptyValue) {
                for (var i = 0; i < aItems.length; i++) {

                    oItem = aItems[i];

                    // the item match with the value
                    bMatch = (oItem[sMutator]().match(new RegExp(sValue, "i")) !== null);

                    if (bMatch) {
                        aFilteredItems.push(oItem);
                    }

                    this._setItemVisibility(oItem, bMatch);
                }
            }
            return aFilteredItems;
        };

然后是我的观点:

<cbc:ComboBoxContain  id="my-id"  selectionChange='onChange'>
                <core:Item key="{key}" text="{text}" />
</cbc:ComboBoxContain >