nestario nestario - 1 year ago 210
JSON Question

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

Answer Source

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
    });
}