Search code examples
sapui5

Fill my List at view.xml with values from js array


I want to fill my List at the view.xml with the values of an Object which includes an array (arr). I get the object before from a SAP software via a web service.

Test.view.xml:

<HBox>
                            
   <List id="select_menu" visible="hidde" mode="SingleSelectMaster" selectionChange="onDetail">
         <StandardListItem title="{Id}" />
   </List>
</HBox>

test.controller.js:

        onTest: function(){
            var itemTemplate = new sap.m.StandardListItem({
                title : "{Id}",             
                icon: "icons/yellow_sign_micro.png",                
                iconInset: false,               
                customData: [               
                new sap.ui.core.CustomData({                
                key: "flag",                
                value: "false"              
                })]
                });
            this.getView().byId("select_menu").bindAggregation("items", {
                path: arr,
                template: itemTemplat               
                })
        }
    });

Solution

  • var myModel = new sap.ui.model.json.JSONModel();
    myModel.setData(yourResponseFromWebservice);
    

    There is no need for creating controls with JS. You can use XML for binding. Write your data in a JSON Model and bind your model with your list via items


     <List items="{myModel>/items}" id="select_menu" visible="hidden" mode="SingleSelectMaster" selectionChange="onDetail">
        <items>
            <StandardListItem icon="icons/yellow_sign_micro.png" title="{myModel>Id}" >
            <customData>
              <core:CustomData key="flag" value="false" writeToDom="true" />
           </customData>
           </StandardListItem>
        </items>
     </List>