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

标签 javascript ajax dojo treegrid

我正在寻找如何将 QueryReadStore(或其他一些存储)与 dojox.grid.LazyTreeGrid 结合使用的示例?

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

我已经将 QueryReadStore 与 dojox.grid.DataGrid 一起使用,效果很好:)

帮助,谢谢。

最佳答案

这是一个冗长的解释/示例,基于我目前正在做的一些事情。 这假设基本熟悉 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).

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

服务器处理程序的工作是返回一个包含以下 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。网格将请求它的 child :

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 行下显示一个 child 。

关于javascript - DOJO:延迟加载 LazyTreeGrid 中的节点 - 寻找示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9143437/

相关文章:

javascript - 如何更改脚本中的参数之一

javascript - Node ,如何终止/停止 Node 进程? Ubuntu 16.04 长期支持版

javascript - Webpack Dev Server 代理不会重定向到 api 后端

java - ajax 将序列化表单和模型列表发布到 Controller 方法

javascript - 将数据从本地SQL Server同步到Internet上的MySQL Server(实时服务器)

javascript - Dojo MD5 散列不是以 base 16 表示法

javascript 删除 "onclick"事件监听器

javascript - 如何从数组中获取唯一的年份?

javascript - $.Ajax 超出最大调用堆栈大小

java - Dojo ajax 调用在 Java 代码完成之前返回?