使用Dojo的dgrid,JsonRest和子行/列(Using Dojo's dgrid,

2019-08-02 07:45发布

我有一个网格,我创建抽出被格式化像这样的JSON数据源:

{"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},

等等。 我创造了这个JSON格式的YUI 2数据表和它工作得很好,因为它包含了一切,我需要做记录的意义。 我所做的与YUI,我无法弄清楚如何与dgrid做的是告诉它使用的游客数组的内容来填充dgrid。 这里是我的dgrid代码:

// 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"});             

有一个简单的方法来告诉dgrid从该子行/阵列画画吗?

Answer 1:

我可以在这里看到多个选项,但最直接的将被写入满足的接口你自己的商店dojo/store/api/Store或只是破解它和子类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);
    }           
});

那么你将需要实例化时,增加一个属性- dataProperty

var store = new CustomStore({
    target: "/visitors/",
    idProperty: "num",
    dataProperty: "visitors"
});

看到它在行动时的jsfiddle: http://jsfiddle.net/phusick/MG9jB/

另一个选项是它到达之前改变响应dojo/store/JsonRest ,所以JsonRest得到什么,它预计,一个数组。 道场1.8提供dojo/request它采用XHR2因此遗憾的是不与工作JsonRest ,但只是优美的缘故:

// 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);
});

在文章中介绍道场/请求一个可以找到一个参考dojox/io/xhrPlugins应该提供针对传统代码类似的功能。 即使不是这样,你可以使用dojo/aspect或可能写自己的内容处理程序来实现相同的。



文章来源: Using Dojo's dgrid, JsonRest and subrows/arrays
标签: json dojo dgrid