Javascript 将具有不同键名的自建JSON数组绑定到sap.m.List
我创建以下JSON数组:Javascript 将具有不同键名的自建JSON数组绑定到sap.m.List,javascript,sapui5,Javascript,Sapui5,我创建以下JSON数组: var oModel = new sap.ui.model.json.JSONModel({ ItemSet: [ { "item1": this.oTextBundle.getText("Flower") }, { "item2": this.oTextBundle.getText("Tree") } ] }); //then I push that model to my dialog this._oDialog.setModel(oMode
var oModel = new sap.ui.model.json.JSONModel({
ItemSet: [
{ "item1": this.oTextBundle.getText("Flower") },
{ "item2": this.oTextBundle.getText("Tree") }
]
});
//then I push that model to my dialog
this._oDialog.setModel(oModel);
然后在对话框中有一个列表,如下所示:
<List id="idList" width="700px" items="{ path:'/ItemSet'}">
<CustomListItem>
<Label id="id1" text="{item1}" width="350px" />
</CustomListItem>
<CustomListItem>
<Label id="id2" text="{item2}" width="350px" />
</CustomListItem>
</List>
您可以为
设置一个键,为值设置另一个键,而不是尝试在您的模型中使用不同的键
例如:
var oModel = new sap.ui.model.json.JSONModel({
ItemSet: [
{ "value": this.oTextBundle.getText("Flower"), "id": "1" },
{ "value": this.oTextBundle.getText("Tree"), "id": "2" }
]
});
this._oDialog.setModel(oModel);
这样,每个条目仍然有一个密钥标识符,您可以轻松地将模型绑定到您的:
oppressedItem.id
将为您提供所选项目的id
。您提供的不是列表绑定的模板,而是您想要的两个项目。Sapui5期望模板是一个且唯一的控件,当提供多个控件时,它只接受最新的控件。
在您的例子中,您最终得到了item2,这仅仅是因为模板中的第二个对象引用了item2
如上所述,将视图更改为:
<List id="idList" width="700px" items="{/ItemSet}">
<CustomListItem>
<Label text="{item}" width="350px" />
</CustomListItem>
</List>
它会起作用的
编辑:列表的隐式聚合为“项”。这意味着上述代码严格等同于
<List id="idList" width="700px" items="{/ItemSet}">
<items>
<CustomListItem>
<Label text="{item}" width="350px" />
</CustomListItem>
</items>
</List>
并且由于绑定了“项目”聚合,SAPUI5将考虑“项目”标签中的任何东西作为模板。
<List id="idList" width="700px" items="{/ItemSet}">
<CustomListItem>
<Label text="{item}" width="350px" />
</CustomListItem>
</List>
<List id="idList" width="700px" items="{/ItemSet}">
<items>
<CustomListItem>
<Label text="{item}" width="350px" />
</CustomListItem>
</items>
</List>