在组合框sapui5中提供一个空白选项

providing a blank option in the combobox sapui5

本文关键字:一个 选项 空白 组合 sapui5      更新时间:2023-09-26

我正在尝试从json中填充SAPUI5中的下拉框

      new sap.ui.commons.DropdownBox(this.createId('inpExpenseType'), {
                                selectedKey: '{ExpenseType/Id}',
                                editable: '{/CanEditPayment}',
                                required: "{/CanEditPayment}",
                                displaySecondaryValues: true,
                                items: {
                                    path: 'parameters>/Benefits',
                                    template: new sap.ui.core.ListItem(this.createId('liExpenseType'), {
                                        key: '{parameters>Id}',
                                        text: '{parameters>Name}',
                                        additionalText: '{parameters>Code}'
                                    })
                                },
                                layoutData: new sap.ui.layout.GridData({span: 'L6 M6 S6'})
                            }).attachChange(oms.app.ModelUtils.handleListItemChange)

现在基于eg的条件。如果(status==='Approved'),我希望在下拉框中只显示一个选项。我使用下面的滤镜来完成这个。

    if(state==='Approved'){
//this is the option I wish to display in the dropdownbox.
        aFilters.push(new sap.ui.model.Filter("Id", sap.ui.model.FilterOperator.EQ, "33"));
    }
    odropdownbox.bindItems('parameters>/Benefits', oTemplate, null, aFilters); 

现在我的问题是,这个单一的选项在下拉框中被默认,所以onChange事件不会被触发。

我尝试默认一个空白项,如下所示

   if(State==='Approved'){
        var oItem = new sap.ui.core.ListItem({
            key: 'dummy',
            text: '',
            additionalText: ''
        });
        odropdownbox.insertItem(oItem,0); 
        odropdownbox.setSelectedKey('dummy');
    }

但这没有任何区别,我看到单个选项被填充为默认值。

我该如何解决这个问题?是否有一些干净的方法来操作json列表?

无需添加新条目

在绑定项目后使用fireChange,这样就没有问题了。

odropdownbox.bindItems('parameters>/Benefits', oTemplate, null, aFilters);
odropdownbox.fireChange();