0

我正在尝试在 Dojo 1.8 中获得 JsonRest 功能。工作加载DataGrid。我已经让 Dojo 客户端成功地与 REST 服务器通信。我拨打电话,我的 DataGrid 标题已填充,但未填充任何数据。来自 REST 调用的响应是……

{"data":{"fundId":"12345","fundDescription":"高风险股票基金","bidPrice":26.8,"offerPrice":27.4,"lastUpdated":"2013-01-23T14:13: 45"}}

我的 Dojo 代码是...




        require([
            "dojo/store/JsonRest",
            "dojo/store/Memory",
            "dojo/store/Cache",
            "dojox/grid/DataGrid",
            "dojo/data/ObjectStore",
            "dojo/query",
            "dojo/domReady!" 
        ], function(JsonRest, Memory, Cache, DataGrid, ObjectStore, query) {

            var restStore, memoryStore, myStore, dataStore, grid;

            restStore =  JsonRest({target:"http://localhost:8080/funds/12345"});
            memoryStore = new Memory();
            myStore = Cache(restStore, memoryStore);

            grid = new DataGrid({
                store: dataStore = new ObjectStore({objectStore: myStore}),

        structure: [
            {name:"Fund Id", field:"fundId", width: "200px"},
            {name:"Description", field:"fundDescription", width: "200px"},
            {name:"Bid Price", field:"bidPrice", width: "100px"},
            {name:"Offer Price", field:"offerPrice", width: "100px"},
            {name:"Last Updated", field:"lastUpdated", width: "200px"}
        ]
            }, "target-node-id"); // make sure you have a target HTML element with this id

            grid.startup();

            query("#save").onclick(function(){
                dataStore.save();
            });
        });

    

为了将数据成功加载到网格中,我缺少什么?

4

2 回答 2

0

我想这可能是由于您的REST-Response's datais not an arraybut an object. 它实际上应该是这样的:

    [{
        "fundId": "12345",
        "fundDescription": "High Risk Equity Fund",
        "bidPrice": 26.8,
        "offerPrice": 27.4,
        "lastUpdated": "2013-01-23T14:13:45"
    }]

ObjectStore期望array一个. 由于我通常不会按照您的方式进行操作,因此我不太确定您必须更改什么。但是你必须确保JSON-Response 无论如何都会给你一个arrayObjectStore也许你必须稍后提交它:

grid = new DataGrid({
    // getting data, after making sure its an array ;)
    store: dataStore = new ObjectStore({objectStore: myStore.get(0)}),
    ...

我不知道您是否出于某些原因必须这样做,但是对于您的示例,据我所知,在此示例中的完成方式应该适合您的需要...

于 2013-01-25T06:50:35.590 回答
0

我有一个类似的问题,我的数据来自 CXF Web 服务,看起来像:

{"Customer":[{"id":1,"name":"John-1"},{"id":2," name":"John-2"}]}

我在这里找到了答案:http: //dojotoolkit.org/documentation/tutorials/1.8/populating_datagrid/而我必须手动从商店中获取我的数据。

....
restStore.query("", {}).then(function(data){    
    dataStore = new ObjectStore({objectStore: new Memory({ data: data['Customer'] })}),

    grid = new DataGrid({
        store: dataStore,
        items:data.items,
        structure: [
            {name:"Customer ID", field:"id", width: "200px"},
            {name:"Customer Name", field:"name", width: "200px"}
        ]
    }, "target-node-id"); // make sure you have a target HTML element with this id
    grid.startup();
}
...



我希望这有帮助。
这是我第一次发帖,所以如果事情有点搞砸,我会很糟糕。

于 2014-02-27T20:33:50.300 回答