【问题标题】:Dojo 1.8 JsonRest and DataGridDojo 1.8 JsonRest 和 DataGrid
【发布时间】:2014-02-28 04:33:50
【问题描述】:

我正在尝试在 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();
            });
        });

    

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

【问题讨论】:

    标签: json rest dojo


    【解决方案1】:

    我猜这可能是由于您的REST-Response 的data 不是array 而是object。它实际上应该是这样的:

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

    ObjectStore 需要 array。因为我通常不会像你那样做,我不太确定你必须改变什么。但是你必须确保JSON-Response 无论如何都会给你一个array。也许您必须稍后将其提交给ObjectStore

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

    我不知道您是否出于某些原因必须这样做,但对于您的示例,就我所见,它在 this example 中的完成方式应该满足您的需求...

    【讨论】:

    • 谢谢……我的响应是由 Spring 的 MVC 注释 REST 支持的默认实现返回的……我想我需要调查一下,让它以正确的响应响应。
    【解决方案2】:

    我遇到了类似的问题,我的数据来自 CXF 网络服务,看起来像:

    {"客户":[{"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();
    }
    ...
    



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

    【讨论】:

    • 感谢您的帮助。我感觉有角放弃了道场。
    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 2013-09-13
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    相关资源
    最近更新 更多