2013-01-23 73 views
0

我试图在Dojo 1.8中获得JsonRest功能。正在加载DataGrid。 我已经让Dojo客户端成功与REST服务器通话。我打电话,我的DataGrid标题被填充,但没有填充数据。来自REST呼叫的响应是...Dojo 1.8 JsonRest和DataGrid

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

我的道场的代码是...




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

    

我缺少的是让成功加载到网格中的数据?

回答

0

我想这可能是由于您的REST - 响应的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做的方式应符合您的需要,据我可以看到... ...

+0

谢谢......我的回复是由Spring的MVC注释的REST支持的默认实现返回的......我需要查看一下以获得响应,并获得正确的响应。 – mortsahl

0

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

{“客户”:[{“ID”:1,”名称“:”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(); 
} 
... 



我希望这有助于。
这是我第一次发布,所以我的不好,如果事情有点棘手。

+0

谢谢你的帮助。我已经意识到放弃角斗的道场。 – mortsahl