0

I have an OnDemandGrid with one column that I want to populate with a custom Dojo widget I built. The data used to populate each of these widgets comes from a Solr query. Since I am expecting possibly thousands of search results, I need to use a JsonRest object to make the queries and handle pagination. Here's what I have so far:

The store:

var store = new JsonRest ({
    target: "/solr/json/response",
});

Creating the grid:

var grid = new (declare([OnDemandGrid, Pagination])) ({
    store: store,
    getBeforePut: false,
    columns: [
        {
             label: "Test",
             field: "first",
             renderCell: myRenderFunction //To render the custom widget
        }
    ]
}, "grid");

grid.startup();

myRenderFunction:

var myRenderFunction = function(object, data, cell) {

    var widget = new MyCustomWidget({
        doc: object,
        foo: bar
    }, cell.appendChild(document.createElement("div"));

    widget.startup();
    return widget;
}

Sample Solr response, in JSON form:

{
    "response":{
        "docs":[
            {
                "foo": "Hello",
                "bar": "World"
            },
            {
                "foo": "Easy as",
                "bar": "ABC"
            },
            {
                "foo": "Simple as",
                "bar": "Do re mi"
            }
        ]
    },
    "highlighting": { ... },
    "numFound": "74",
    "start": 0
}

I have followed a few examples online demonstrating how to do this using JsonRest and any of the dgrid flavors (and they all worked), but when I try to render the widget to the grid nothing shows up and I get a TypeError: transform(...) is null.

Is there any reason why I can't render my widget to the grid?

4

1 回答 1

2

我在尝试将 Solr 结果与 dgrid 和 JsonRest 一起使用时遇到了同样的问题。
JsonRest使用 QueryResults 作为它返回的包装器。
您的问题是QueryResults只接受数组或承诺,而您目前正在给它一个对象。

为了给 QueryResultsdocs数组,编写一个自定义的 JsonRest 存储,类似于:

define([
    "dojo/Deferred", "dojo/io-query", "dojo/_base/declare", "dojo/request/xhr",
    "dojo/store/JsonRest", "dojo/store/util/QueryResults"
], function (Deferred, ioQuery, declare, xhr, JsonRest, QueryResults) {
    return declare([JsonRest], {
        target: '/solr/json/response',
        idProperty: 'foo',
        query: function (query, options) {
            var results, total, count = options.count, start = options.start;
            if (start > 0 || count >= 0) {
                query.start = start;
                query.rows = ((options.hasOwnProperty('count') &&
                    count !== Infinity) ? count : 25);
            } else {
                console.error('Missing start and count arguments');
                return;
            }
            results = new Deferred();
            results.total = new Deferred();
            xhr(this.target, {
                query: ioQuery.objectToQuery(query),
                handleAs: 'json',
                headers: {
                    Accept: this.accepts
                }
            }).then(function (data) {
                total = data.response.numFound;
                results.total.resolve(total);
                results.resolve(data.response.docs);
            }, function (e) {
                console.error(e.response.status + '. ' + e.message);
            });
            return new QueryResults(results);
        }
    });
});

我还建议等到正确填充 dgrid 后再使用自定义 renderCell 函数。

编辑: OnDemandGrid 不适用于分页扩展。
所以决定你是想要谨慎的分页控件还是无限滚动(由 dgrid 处理的分页)。
请参阅分页OnDemand文档。

于 2013-12-03T19:00:49.990 回答