user1518485
user1518485

Reputation: 65

Dojo Enhanced Datagrid not populating with JsonRest store

My Datagrid is not populating the store

My rest call returns this: {"onlineUsers":[],"offlineUsers":["123.456.7.890:8080"]}

My code:
require([  "dojo/store/JsonRest",
        "dojo/store/Memory",
        "dojo/store/Cache",
        "dojox/grid/EnhancedGrid",
        "dojox/grid/enhanced/plugins/Pagination",
        "dojo/data/ObjectStore",
        "dojo/dom-attr",
        "dojo/domReady!"],
        function(JsonRest, Memory, Cache, EnhancedGrid, Pagination, ObjectStore, domAttr){

        var store = new dojo.store.JsonRest({target:"/Url/rest/onlineusers/all"});

        var dataStore = new dojo.data.ObjectStore({objectStore: store});

        var grid = new EnhancedGrid({
            id: 'grid',
            store: dataStore,
            structure: [
          {name: "Offline Users", field: "offlineUsers", width: "100px"}
        ]
        }, "gridDiv");

            grid.startup();
    });

I see this in the logs: [object Object]

i also see this: [18:12:57.822] Use of getAttributeNode() is deprecated. Use getAttribute() instead. @ http://ajax.googleapis.com/ajax/libs/dojo/1.8.4/dojo/dojo.js:146

what am i doing wrong?

Upvotes: 0

Views: 729

Answers (1)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44685

That's because your data structure is wrong. I suppose you want to create a grid with each host/address listed as a row in the grid?

The data structure of a store is supposed to be an array of objects. You defined an object with arrays of strings, which is not compliant with the wanted structure. More information about the format your REST service should implement can be found at the RFC 2616 document.

You have three choices:

  1. Change your RESTful webservice to return your data in a way that Dojo can handle it
  2. Write your own store implementation
  3. Manually parse your data clientside by manually defining an AJAX request and mapping the object

The third solution would look something like:

var data =  {"onlineUsers":[],"offlineUsers":["123.456.7.890:8080"]}; // Retrieve with an AJAX request
var offlineUsers = array.map(data.offlineUsers, function(user) {
    return {
        host: user  
    };
});
var store = new Memory({ data: offlineUsers });
var dataStore = new ObjectStore({objectStore: store});

Then you can change the structure property of your grid to:

[
    {name: "Offline Users", field: "host", width: "100px"}
]

I also made a JSFiddle of the example above, which you can see here.

Upvotes: 1

Related Questions