SAPUI5: Custom filtering contains for a dynamic binding from controller

懵懂的女人 提交于 2020-01-25 09:32:18

问题


I would like to use a custom filtering contains like here : https://sapui5.hana.ondemand.com/#/entity/sap.m.ComboBox/sample/sap.m.sample.ComboBoxFilteringContains
but problem is oLocation.setFilterFunction is not a function :(
items are not defined into combobox :

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

because they are define in controller :

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"))
            });

Does someone has a solution ?


回答1:


I find the solution:
I created a control ComboBoxContain which override ComboBox filterItems as follow :

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;
        };

and then my view :

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


来源:https://stackoverflow.com/questions/59268503/sapui5-custom-filtering-contains-for-a-dynamic-binding-from-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!