nestario
nestario

Reputation: 117

Use Local JSON File for Databinding in SAPUI5

I have a little Problem with SAPUI5. I have the following Code:

var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData("JSON/saplogon.json");


    var oTable = new sap.ui.table.Table({
        visibleRowCount: 30,
        firstVisibleRow: 0
    });


    oTable.addColumn(new sap.ui.table.Column({
        label: new sap.ui.commons.Label({text: "Server"}),
        template: new sap.ui.commons.TextView().bindProperty("text", "Server"),
        width: "40%"
    }));


    oTable.addColumn(new sap.ui.table.Column({
        label: new sap.ui.commons.Label({text: "Beschreibung"}),
        template: new sap.ui.commons.TextView().bindProperty("text", "Description"),
        width: "40%"
    }));

    oTable.addColumn(new sap.ui.table.Column({
        label: new sap.ui.commons.Label({text: "Adresse"}),
        template: new sap.ui.commons.TextView().bindProperty("text", "Address"),
        width: "10%"
    }));

    oTable.addColumn(new sap.ui.table.Column({
        label: new sap.ui.commons.Label({text: "SystemID"}),
        template: new sap.ui.commons.TextView().bindProperty("text", "mssysname"),
        width: "10%"
    }));


    oTable.setModel(oModel);
    oTable.bindRows({
        path: "/Systeme",
        filter: allFilter
    });     

    oPage_Results.addContent(oTable);

the Code above works but when I change the directory of the .JSON File to a Directory on my Server it wont work, I have also tried to get the data from another Server with JSONP but that also wont work, can anybody please help me to find a way for consuming a local JSON file from my server which is not in my package.

Thank you very much guys

Upvotes: 1

Views: 13644

Answers (3)

chiranjeevigk
chiranjeevigk

Reputation: 1666

Better way of loading the JSON file in UI5

var oModel = new JSONModel()
oModel.loadData(pathToTheJSONFile);
oModel.attachRequestCompleted(function(oEventModel){
    //console.log(oModel.getData());
    //This is called after data is loading
});
oTable.setModel(oModel);

i feel this is better way.

Upvotes: 2

nestario
nestario

Reputation: 117

Finally it worked now, i just installed an IIS7 Webserver on the server of my SAP ERP System and hosted my file over the IIS7. I didnt needed to use an AJAX request or something the normal load model method of the SAPUI5 library worked. Thanks for your help mates.

Upvotes: 0

dotchuZ
dotchuZ

Reputation: 2641

Try to Ajax the file, after defining the table and its columns:

var sServiceUrl = 'yourServerOrNetworkPathInclJson';
var post = $.ajax({
            url : sServiceUrl,
            type : "GET"
        });

post.done(function(data) {

    var oModel = new sap.ui.model.json.JSONModel();
    console.log(data); // check binding path
    oModel.setData(data);

    oTable.setModel(oModel);
    oTable.bindRows({
        path : "/", // might also be "/d/results" or whatever
    });
}

Upvotes: 0

Related Questions