如何在ui5中为列表设置包含多个项目的模板

时间:2019-03-29 08:46:24

标签: list templates sapui5

在我的UI5应用中,有一个列表,我想在其中显示多个DisplayListItem。因此,我为列表设置了一个模板以与
绑定 oList.bindAggregation("items", "/my_path", oListTemplate);

如果我像这样构建模板:
oListTemplate = new sap.m.DisplayListItem(...);
一切正常。
但是现在我需要将多个new sap.m.DisplayListItem(...)和一个像oListTempate = [new sap.m.DisplayListItem(...), new sap.m.DisplayListItem(...),..];这样的数组放入一个模板中。如果这样做,我会因未提供模板而收到错误消息: Error: Missing template or factory function for aggregation items

使用模板是否可以提供多个项目。在sap文档中:https://sapui5.hana.ondemand.com/1.34.9/docs/guide/91f057786f4d1014b6dd926db0e91070.html有一行:

  

如上例所示,模板不一定是单个控件,也可以是控件树。

因此,我认为有可能,但我不知道该怎么做。

预先谢谢你

2 个答案:

答案 0 :(得分:0)

浏览结果并为每个结果添加一个DisplayListItem:

var aItems = [];
oResponse.results.forEach(function(oResult){
  aItems.push(new sap.m.DisplayListItem({
                    label: "oResult.name"
                })
  );
});

将数组添加到列表中

var oList = new sap.m.List({
    headerText: "Test list",
    items: aItems
});

答案 1 :(得分:0)

指定模板的正确方法是:

oList.bindAggregation("items", {
path: "/my_path",
template: oListTemplate
});

通过创建单个项目将项目添加到列表中可能会导致性能问题。