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 を提供するか、フェッチ関数を上書きすることにより、クエリ パラメータを介して自分でページネーションを処理できますが、配列の代わりにオブジェクトを受け入れるのは面倒です。このようなオブジェクトを解析するには、データ ストアをサブクラス化し、カスタム _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 に答える