2

我正在从我的 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}]}

有任何想法吗?谢谢。

4

2 回答 2

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}]

注意数组表示法。

于 2010-03-17T14:38:35.210 回答
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 函数:

dojo.declare("dojox.data.CustomStore", dojox.data.JsonRestStore, {
    _processResults: function(results, deferred){
        var items = SOME_MAPPING_FUNCTION_HERE(results);
        var count = COUNT_RESULTS_HERE;
        return {totalCount:deferred.fullLength || (deferred.request.count == count ? (deferred.request.start || 0) + count * 2 : count), items: items};
    }
}

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

于 2010-03-16T09:22:17.360 回答