问题
I have a grid that I am creating drawing off a JSON data source that is formatted like this:
{"recordsReturned":10,
"totalRecords":471,
"startIndex":0,
"sort":"num",
"dir":"asc",
"pageSize":100,
"visitors":[
{"num":1, "uid": "1", "ipaddress": "24.217.129.98", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.14 (KHTML, like Gecko) Version/6.0.1 Safari/536.26.14", "date":1352086661000},
{"num":2, "uid": "0", "ipaddress": "100.43.83.157", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)", "date":1351761442000},
{"num":3, "uid": "0", "ipaddress": "100.43.83.157", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)", "date":1351718948000},
{"num":4, "uid": "0", "ipaddress": "100.43.83.157", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)", "date":1350349829000},
{"num":5, "uid": "0", "ipaddress": "70.36.100.148", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (compatible; MJ12bot/v1.4.3; http://www.majestic12.co.uk/bot.php?+)", "date":1349718631000},
{"num":6, "uid": "0", "ipaddress": "180.76.5.153", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)", "date":1349396285000},
{"num":7, "uid": "0", "ipaddress": "76.72.166.150", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (compatible; MJ12bot/v1.4.3; http://www.majestic12.co.uk/bot.php?+)", "date":1349090589000},
{"num":8, "uid": "0", "ipaddress": "65.55.52.115", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)", "date":1348417348000},
{"num":9, "uid": "0", "ipaddress": "66.249.72.195", "hostname": "", "referer": "", "useragent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "date":1348353989000},
And so on. I created this JSON format for a YUI 2 DataTable and it works well, since it contains everything I need to make sense of the record. What I did with YUI that I cannot figure out how to do with dgrid is to tell it to use the contents of the visitors array to populate the dgrid. Here is my dgrid code:
// Create a new constructor by mixing in the components
var CustomGrid = declare([ OnDemandGrid, Keyboard, Selection ]);
var grid = new declare([OnDemandGrid, Keyboard, Selection])({
store: store,
columns: {
num: "ID",
uid: "visitorsUID"
},
/*selectionMode: "single", // for Selection; only select a single row at a time
cellNavigation: false // for Keyboard; allow only row-level keyboard navigation*/
}, "grid");
grid.setQuery({aid: "1604", sort: "num", dir: "asc", startIndex: "0", results: "100"});
Is there a simple way to tell dgrid to draw from that subrow/array?
回答1:
I can see multiple options here, but the most straightforward would be writing your own Store satisfying the interface of dojo/store/api/Store
or just hack it and subclass dojo/store/JsonStore
:
var CustomStore = declare(JsonRest, {
query: function(query, options) {
var dataProperty = this.dataProperty;
var results = this.inherited(arguments);
var deferred = results.then(function(result) {
return result[dataProperty];
});
return QueryResults(deferred);
}
});
then you will need to add one more property when instantiating - dataProperty
:
var store = new CustomStore({
target: "/visitors/",
idProperty: "num",
dataProperty: "visitors"
});
See it in action at jsFiddle: http://jsfiddle.net/phusick/MG9jB/
Other option would be to change the response before it reaches dojo/store/JsonRest
, so JsonRest
gets what it expects, an array. Dojo 1.8 provides dojo/request which employs XHR2 thus it unfortunately does not work with JsonRest
, but just for the sake of elegancy:
// var request = require("dojo/request/registry");
// var xhr = require("dojo/request/xhr");
var handle = request.register(/(.*)\/visitors.json$/, function(url, options) {
// if any XHR request url ends with `visitors.json` then return
// `visitors` property
return xhr.get(url, options).then(function(results) {
return results["visitors"];
});
});
request.get("app/visitors.json", {handleAs: "json"}).then(function(visitors) {
console.log(visitors);
});
In the article Introducing dojo/request one can find a reference to dojox/io/xhrPlugins
which should provide similar functionality against legacy code. And even if it does not, you can use dojo/aspect or possibly write your own content handler to achieve the same.
来源:https://stackoverflow.com/questions/13226264/using-dojos-dgrid-jsonrest-and-subrows-arrays