Search code examples
javascriptodatasapui5

Dynamically bind table from controller in SAPUI5


I have created a table in the xml view, I would like to bind the table in the controller so it can be modified dynamically depending on who is loading the app.

XML VIEW

<Table inset="false"
            id="pTable">    
            <columns>
                <Column id="year" width="auto">
                    <Text text="Year" />
                </Column>
                <Column id="rating" width="auto">
                    <Text text="Performance Rating"/>
                </Column>
                <Column id="respect" width="auto">
                    <Text text="Managing with Respect" />
                </Column>
            </columns>
            <items>
                <ColumnListItem>
                    <cells>
                        <Text id="tYear" text="{Begda}" />
                        <Text id="tRating" text="{Rating}" />
                        <Text id="tRespect" text="{MwrRating}" />                        
                    </cells>
                </ColumnListItem>
             </items>
            </Table>     

JS Controller

var pHistory = this.byId("pTable");
var phURL = "/PMRPerformanceSet/?$filter=IvGuid eq '5438A43913276540E1008000A7E414BA'"
pHistory.setModel(oModel);
pHistory.bindRows(phURL);

It seems like the controller should look something like this, however, this does not work

Any suggestions?


Solution

  • I guess you are using sap.m.Table:

    <Table id="pTable" 
        inset="false" 
        items="{/PMRPerformanceSet}">  // add items Here for your oData  collection   
        <columns>
            <Column id="year" width="auto">
                <Text text="Year" />
             </Column>
             <Column id="rating" width="auto">
                 <Text text="Performance Rating"/>
             </Column>
             <Column id="respect" width="auto">
                 <Text text="Managing with Respect" />
             </Column>
         </columns>
         <items>
             <ColumnListItem>
                 <cells>
                     <Text id="tYear" text="{Begda}" />
                     <Text id="tRating" text="{Rating}" />
                     <Text id="tRespect" text="{MwrRating}" />                        
                 </cells>
             </ColumnListItem>
         </items>
    </Table>
    

    Then set Model in controller. check your odata service response in Network tab of developers tool then Bind items according to that in XML view.