QueryReadStore将JSON加载到DataGrid中,但JsonRestStore不加载(来自同一个源)

时间:2010-03-15 02:45:43

标签: javascript rest datagrid dojo datastore

我正在从我的REST接口提供的JSON数据构建Dojo DataGrid。 DataGrid使用QueryReadStore加载数据很好,但似乎不能使用通过管道输入JsonRestStore的相同数据。

我在Dojo 1.4.1中使用以下Dojo库:

dojo.require("dojox.data.JsonRestStore");
dojo.require("dojox.grid.DataGrid");
dojo.require("dojox.data.QueryReadStore");
dojo.require("dojo.parser");

我以下列方式声明我的商店:

var storeJRS = new dojox.data.JsonRestStore({target:"api/collaborations.php/1"});
var storeQRS = new dojox.data.QueryReadStore({url:"api/collaborations.php/1", requestMethod:"get"});

我创建了这样的网格布局:

var gridLayout = [
new dojox.grid.cells.RowIndex({ name: "Row #", width: 5, styles: "text-align: left;" }),
{
name: "Name",
field: "name",
styles: "text-align:right;",
width:20
},
{
name: "Description",
field: "description",
width:30
}
];

我按如下方式创建我的DataGrid:
    <div dojoType="dojox.grid.DataGrid" jsid="grid2" store="storeQRS" structure="gridLayout" style="height:500px; width:1000px;"></div>

以上工作,但如果我使用QueryReadStore作为我的商店,网格是使用标题(名称,描述)创建的,但它没有填充任何行:
<div dojoType="dojox.grid.DataGrid" jsid="grid3" store="storeQRS" structure="gridLayout" style="height:500px; width:1000px;"></div>

使用FireBug,我可以看到QueryReadStore从我的REST接口获取我的JSON数据。它看起来如下:

{"numRows":6,"items":[{"name":"My Super Cool Collab","description":"This is for all the super cool people in the super cool group","id":1},{"name":"My Other Super Cool","description":"This is for all the other super cool people","id":3},{"name":"This is another coll","description":"This is just some other collab","id":4},{"name":"some new collab","description":"this is a new collab","id":5},{"name":"yet another new coll","description":"uh huh","id":6},{"name":"asdf","description":"asdf","id":7}]}

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:4)

要使用JsonRestStore,您的响应必须只是您提供的样本的项目部分:

[{"name":"My Super Cool Collab","description":"This is for all the super cool people in the super cool group","id":1},{"name":"My Other Super Cool","description":"This is for all the other super cool people","id":3},{"name":"This is another coll","description":"This is just some other collab","id":4},{"name":"some new collab","description":"this is a new collab","id":5},{"name":"yet another new coll","description":"uh huh","id":6},{"name":"asdf","description":"asdf","id":7}]

注意数组符号。

答案 1 :(得分:2)

JsonRestStore的默认服务处理程序使用标头请求一系列项目,并期望返回所请求的项目数组,而不是对象。处理请求一系列项目的代码是:

if(args && (args.start >= 0 || args.count >= 0)){
    request.headers.Range = "items=" + (args.start || '0') + '-' + ((args.count && args.count != Infinity && (args.count + (args.start || 0) - 1)) || '');
}

如果你愿意,你可以通过查询参数自己处理分页,提供自定义getRequest或覆盖fetch函数......但接受一个对象而不是一个数组将是痛苦的。解析这样的对象将要求您子类化数据存储并提供自定义_processResults函数:

if(args && (args.start >= 0 || args.count >= 0)){
    request.headers.Range = "items=" + (args.start || '0') + '-' + ((args.count && args.count != Infinity && (args.count + (args.start || 0) - 1)) || '');
}

仅处理解析响应...我不确定是否可以轻松更改发布的内容。