我正在尝试在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" }}
我的道场代码是......
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 :(得分: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
- 响应为你提供array
。也许你必须像以后那样将它提交到ObjectStore
:
grid = new DataGrid({
// getting data, after making sure its an array ;)
store: dataStore = new ObjectStore({objectStore: myStore.get(0)}),
...
我不知道你是否因为某些原因必须这样做,但对于你的例子,它在this example中完成的方式应该符合你的需要......就我所见......
答案 1 :(得分:0)
我有类似的问题,我的数据来自CXF网络服务,看起来像:
{ “客户”:[{ “ID”:1, “名称”: “约翰-1”},{ “ID”:2 “名称为”: “约翰-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();
}
...
我希望这会有所帮助
这是我第一次发帖,所以如果事情有点棘手的话,那就太糟糕了。