0

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?

4

1 回答 1

1

那是因为你的数据结构是错误的。我想你想创建一个网格,每个主机/地址在网格中列为一行?

商店的数据结构应该是一个对象数组。您使用字符串数组定义了一个对象,该对象不符合所需的结构。有关 REST 服务应实现的格式的更多信息,请参阅RFC 2616 文档

你有三个选择:

  1. 更改您的 RESTful Web 服务,以 Dojo 可以处理的方式返回您的数据
  2. 编写自己的商店实现
  3. 通过手动定义 AJAX 请求并映射对象来手动解析您的数据客户端

第三个解决方案看起来像:

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

然后您可以将structure网格的属性更改为:

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

我还制作了上面示例的 JSFiddle,您可以在此处查看

于 2013-10-23T10:25:03.327 回答