我创建以下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(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>


最后,唯一绑定的值是来自item2,将“ Tree”的i18n转换加载到我的列表中,但未出现“ Flower”。当我切换到以下代码时:



var oModel = new sap.ui.model.json.JSONModel({
  ItemSet: [{
      "item": this.oTextBundle.getText("Flower")
    },
    {
      "item": 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="{item}" width="350px" />
  </CustomListItem>
  <CustomListItem>
    <Label id="id2" text="{item}" width="350px" />
  </CustomListItem>
</List>





所以所有JSON对象中的键名都必须相同吗?还是有一种方法可以使用数组中的不同键来实现呢?

最佳答案

可以尝试为ID使用另一个键,而对于values使用另一个键,而不是尝试在模型中使用其他键。

一个例子:

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


这样,您仍然为每个条目都有一个密钥标识符,并且可以轻松地将模型绑定到您的sap.m.List

<List mode="SingleSelectMaster" items="{ path:'/ItemSet'}" selectionChange="listPress">
  <CustomListItem>
    <Label text="{value}"/>
  </CustomListItem>
</List>


然后,您可以使用以下方法获取所选项目的ID

listPress: function (oControlEvent) {
   var oCtx = oControlEvent.getSource().getBindingContext();
   var oPressedItem = this.getView().getModel().getProperty(oCtx.getPath());
}


oPressedItem.id将为您提供所选项目的ID

10-07 14:10
查看更多