UI5 - 如何根据另一个组合框将数据动态绑定到 Table 中的 Select?

UI5 - how to dynamically bind data to a Select in Table, depending on another combobox?

我有一个典型的情况 - table 有两个组合框(或者,确切地说,sap.m.Select 控件),在第一个 select 之后,我想更新第二个中的值。这是我的模型,基本上,第一个组合框应该包含可用状态的列表,一旦 selected,第二个 sap.m.Select 控件应该由相关城市填充

{
 countries: [
 { country: 'France', cities: ['Paris', 'Marseille', 'Lyon']},
 { country: 'Germany', cities: ['Berlin', 'Bonn']}
]
}

问题是我不知道该怎么做。我可以使用类似这样的方法获取更新行的 ID。

onCountryChange: function (oEvent) {
    const selectedItem = oEvent.getParameter("selectedItem");
    var path = selectedItem.getBindingContext().getPath();
    bindComboBox(path); // this should rebind the data, but not written yet
}

我现在知道我应该在正确的组合框中重新绑定数据,但是,我不知道如何只影响正确行上的单个组合框以及如何更新它。有人可以建议我该怎么做吗?整个 table 是在 .xml 视图中定义的,我也可以使用格式化程序或内联表达式来实现吗?还是这种情况对它来说太难了?

谢谢

您可以使用 bindAggregation 方法(来自 ManagedObject)class 重新绑定组合框的项目。

onCountryChange: function (oEvent) {
    const selectedItem = oEvent.getParameter("selectedItem");
    var path = selectedItem.getBindingContext().getPath();

    this.byId("combo2").bindAggregation("items", {
        path: path + "/cities",
        template: new sap.ui.core.Item({
            key: "{}",
            text: "{}"
        })
    });
}

注意:将 "combo2" 替换为您的第二个组合 box/select 控件的 ID。

编辑:要获得正确的组合框(假设您在 table 上创建了多个组合框,请使用第一个组合框 (oEvent.getSource().getId()) 的 ID 生成第二个组合框的 ID框。如果不了解 table 的更多结构(及其创建方式),我无法提供更多。