polaris
polaris

Reputation: 339

How to bind OData directly to XMLView

I have an UI5 app in which I have a table defined in my XMLView. I'm making a call to the backend using OData to retrieve the data. I'm doing it the following way.

 var oModel = new sap.ui.model.odata.ODataModel("url to data", true);
            var inputModel = new JSONModel();

            oModel.read("/Products",
                null,
                null,
                false,
                function _OnSuccess(oData, response) {
                    var data = oData.results;
                    inputModel.setData(data);
                },
                function _OnError(error) {
                    console.log(error);
                });

            //set model(s) to current xml view
            this.getView().setModel(inputModel, "inputModel");

How can I do this without having to create the JSON model, I mean bind the oData directly to the XMLView.

I have seen it being done but only with JSView, e.g:

var oModel = new sap.ui.model.odata.v2.ODataModel("http://admin-   think:88/sap/...",{useBatch : true});
  sap.ui.getCore().setModel(oModel,"model1");

// Create instance of table 
var oTable = new sap.ui.table.Table({
visibleRowCount : 6,
    selectionMode: sap.ui.table.SelectionMode.Single, 
    navigationMode: sap.ui.table.NavigationMode.scrollbar, 
    selectionBehavior: sap.ui.table.SelectionBehavior.RowOnly
});

// First column "Application"
oTable.addColumn(new sap.ui.table.Column({
       label : new sap.ui.commons.Label({
             text : "APPLICATION",
             textAlign : "Center",

       }),
       template : new sap.ui.commons.TextView({
            textAlign:"Center"}).bindProperty("text","model1>Applno"),              
}));

// Bind model to table control    
 oTable.bindRows("model1>/");

This way it seems like a lot of work. How can I do something like this but using an XML view?

Upvotes: 0

Views: 5553

Answers (1)

vivek
vivek

Reputation: 140

XML Code :

<mvc:View
    controllerName="sap.m.sample.Table.Table"
    xmlns:l="sap.ui.layout"
    xmlns:mvc="sap.ui.core.mvc"
    xmlns="sap.m">
    <Table id="idProductsTable"
        inset="false"
        items="{Data>/Table}">
        <columns>
            <Column>
                <Text text="Name" />
            </Column>
            <Column>
                <Text text="id" />
            </Column>
        </columns>
        <items>
            <ColumnListItem>
                <cells>
                    <Text
                        text="{Data>name}" />
                    <Text
                        text="{Data>id}" />
                </cells>
            </ColumnListItem>
        </items>
    </Table>
</mvc:View>

JS Code :

onAfterRendering : function(){
   var oView = this.getView();
   var oTableJSON = new sap.ui.model.json.JSONModel();
   var fnSuccess = function(oEvent,oResponse){
      var Data = {
          Table : oData.results,
      };
      oTableJSON.setData(Data);   
      oView.byId("idProductsTable").setModel(oTableJSON,"Data");
   };
   oModel("/ProductionSet",null,null,true,fnSuccess,fnFail);
}

Upvotes: 1

Related Questions