DOJO:LazyTreeGrid中的延迟加载节点 - 查找示例代码

时间:2012-02-04 18:46:35

标签: javascript ajax dojo treegrid

我正在寻找一个如何在dojox.grid.LazyTreeGrid中使用QueryReadStore(或其他商店)的示例?

我希望能够显示大型结构并仅从服务器加载必要的必需数据。 应该只从专用服务器脚本加载开放节点的子节点。

我已经在dojox.grid.DataGrid中使用了QueryReadStore并且效果很好:)

帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

根据我目前正在做的一些事情,这是一个冗长的解释/样本。 这假设使用Dojo 1.7风格的软件包是基本的舒适(例如,我们假设默认的Dojo软件包已正确设置)。

客户端(js文件)

require(["dojo/ready",
    "dojox/grid/LazyTreeGridStoreModel",
    "dojox/data/QueryReadStore",
    "dojox/grid/LazyTreeGrid"], function(ready, LazyTreeGridStoreModel, QueryReadStore, LazyTreeGrid) {
        ready(function() {
            var cellLayout = [
                {name: 'Name of fruit', field: 'name', width: '300px', defaultValue: ""},
                {name: 'Color of fruit', field: 'color', width: '100px', defaultValue: ""},
                {name: 'Size of fruit', field: 'size', width: '100px', defaultValue: ""}
            ];

            // This is the url on which your server will listen for GET requests
            var dataStore = new QueryReadStore({url: "url/to/load/rows"});
            var treeModel = new LazyTreeGridStoreModel({store: dataStore, childrenAttrs: ['children'], serverStore: true});

            var grid = new LazyTreeGrid({
                treeModel: treeModel,
                structure: cellLayout,
                autoHeight: 20}, "gridContainerId"); // you need to have a DOM element by that id
            grid.startup();
        }
    });

服务器端:

您需要一个服务器端处理程序来监听url/to/load/rows上的GET请求。这些请求最多有3个参数:

start    - 0-based index of the first item to return
count    - number of items to return
parentId - Id of the parent of the children items that are requested.
           Optional, not present for 1st call (because 1st-level objects have no parents).

该处理程序可以用您最喜欢的服务器端语言编写(即使用ASP.Net MVC,Ruby等编写C#)

服务器处理程序的工作是返回包含以下3个属性的json结构:

items       - Array containing the items you want to display.
              Each item represents a row in the grid. Each item should
              have at least some of the fields you specified in your layout
              and must have the 2 following characteristics:
                - Must have a "children" attribute. That is a bool value.
                  If true, the row is assumed to have children and will have
                  an expando left of it. The grid query the server for children when you expand it.
                  If false, the row is considered terminal, no expando is shown.

                - Must have a unique id. This will be the id that will be set in the "parentId"
                  param when querying the server for the children of that row. It must be stored
                  in the field referred to by the "identifier" attribute (see below).

identifier  - The name of the attribute of each item that contains its unique id.
numRows     - The number of total items existing on this level (not just the number of items you sent back).
              This is used to set the grid & scrollbar size and other UI things.

客户端/服务器通信

基于我之前的示例,一旦网格启动(客户端),它将请求类似的内容:

GET url/to/load/rows?start=0&count=25

服务器将返回以下内容:

{
    "items":[
        {"name": "apple", "color": "red", "size": "small", "uniqueId":"a1", "children": true},
        {"name": "watermelon", "color": "green", "size": "big", "uniqueId":"b1", "children": false}
    ],
    "identifier": "uniqueId",
    "numRows":2
}

网格将显示2个水果。 apple将有一个expando,但不是watermelon(由于children属性)。 假设用户点击了apple expando。网格将请求其子女:

GET url/to/load/rows?parentId=a1&start=0&count=25

服务器可以返回类似的内容:

{
    "items":[
        {"name": "mcintosh", "color": "red-green", "size": "small", "uniqueId":"a2", "children": false}
    ],
    "identifier": "uniqueId",
    "numRows":1
}

然后,网格将在apple行下显示一个孩子。

答案 1 :(得分:0)

我想我在这里有你想要的东西。关于使用带有dojox.grid.LazyTreeGrid的QueryReadStore的一些优秀示例代码,它也是一步一步完全解释的。

请参见此处:http://livedocs.dojotoolkit.org/dojox/grid/LazyTreeGrid

我希望这可以促进你的工作,并且你能够实现你的目标。

此致

弗兰克。