Kirit Modi
Kirit Modi

Reputation: 23407

How to Bind json object in array sapui 5?

My file controller.js,

Now want to bind json object in array using sapui 5. I am try below code for it.

onInit: function() {
        var elementArray=['ID','Name','Description','ReleaseDate','DiscontinuedDate','Rating','Price'];
        var oModel = new sap.ui.model.json.JSONModel(elementArray);
        sap.ui.getCore().setModel(oModel,'fieldArray');

//      var oModel = new sap.ui.model.json.JSONModel('./smartappall/door.josn');
//      sap.ui.getCore().setModel(oModel,'approcords');
    },

view.js

var elementArray= bind fieldArray json model here

Upvotes: 2

Views: 11162

Answers (2)

Ji aSH
Ji aSH

Reputation: 3457

The JSONModel data must be a plain javascript object. IE not a javascript array.

You can either follow the previous answer and set your array as a property of the JSONModel, or modify your initial array to wrap it inside an object :

onInit: function() {
    var data = { elementArray: [
        'ID',
        'Name',
        'Description',
        'ReleaseDate',
        'DiscontinuedDate',
        'Rating',
        'Price'
    ]};
    var oModel = new sap.ui.model.json.JSONModel(data);
    sap.ui.getCore().setModel(oModel,'fieldArray');

},

Then you can bind your view to {fieldArray>/elementArray}

Upvotes: 0

Qualiture
Qualiture

Reputation: 4920

I'm not sure what you are trying to achieve, but since your model only contains the array, in your view you can do the following:

var elementArray = sap.ui.getCore().getModel("fieldArray").getData();

(EDITED: Forgot the getData() part...)

But generally, you don't store objects or arrays in a dedicated model, but rather have one model where you store them in separate properties. In that case, you can do:

In controller:

sap.ui.getCore().getModel("fieldArray").setProperty("/pathToYourArray", elementArray");

In view:

var elementArray = sap.ui.getCore().getModel("fieldArray").getProperty("/pathToYourArray");

Upvotes: 2

Related Questions