Search code examples
listtemplatessapui5

How to setup a template with multiple items for a list in ui5


in my UI5 App, there is a list, in which I want to show more than one DisplayListItem. Therefore I set up a template for the list to bind with
oList.bindAggregation("items", "/my_path", oListTemplate);

If i build the template like this:
oListTemplate = new sap.m.DisplayListItem(...);
Anything works perfekt.
But now I need to give several new sap.m.DisplayListItem(...) with an array like oListTempate = [new sap.m.DisplayListItem(...), new sap.m.DisplayListItem(...),..]; into one template. If I do so, I get an error for not having a template given: Error: Missing template or factory function for aggregation items

Is it not possible to give more than one item with the template. At the sap docu: https://sapui5.hana.ondemand.com/1.34.9/docs/guide/91f057786f4d1014b6dd926db0e91070.html there is the line:

A template is not necessarily a single control as shown in the example above, but can also be a tree of controls.

Because of this, I think it is possible, but I don't know how to do it.

thank you in advance


Solution

  • Loop through your results and add a DisplayListItem for every result:

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

    Add your Array to your List:

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