通过ID获取声明性dojo数据网格

Get declarative dojo datagrid by ID

本文关键字:数据 数据网 网格 dojo ID 获取 声明 通过      更新时间:2024-02-11

我在按ID访问声明创建的数据网格时遇到问题,因此无法设置其数据存储。

这是我的代码,但树返回为未定义

提前感谢您的帮助。

<!DOCTYPE html>
<html lang="en">
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.3/dojo/dojo.js" data-dojo-config="async: true"></script>
    </head>
    <body>
        <table data-dojo-id="myTree" dataType="dojox.grid.TreeGrid" summary="This is a test">
            <thead>
                <tr>
                    <th field="a" width="200px">A</th>
                    <th field="items" aggregate="sum" itemAggregates="count">
                        <table>
                            <thead>
                                <tr>
                                    <th field="name" width="200px">Name</th>
                                    <th field="count" width="200px">Count</th>
                                </tr>
                            </thead>
                        </table>
                    </th>
                </tr>
            </thead>
        </table>
    </body>
    <script>
        require(["dijit/registry", "dojo/data/ItemFileReadStore"], function( Registry, ReadStore ) {
            var store = new ReadStore();
            var tree = Registry.byId("myTree");
            console.log(tree);
            // tree.setStore( store );
        });
    </script>
</html>

您的代码有几个问题导致您的问题。

  • dataType不是一个东西;您想要data-dojo-type(可能会将其与不推荐使用的dojoType混淆)
  • 设置data-dojo-id会创建一个全局变量,而不是Dijit注册表的ID;改为设置id
  • 您也从来没有在文档上实际运行过dojo/parser,所以即使解决了这些问题,您也不会得到真正的小部件

这里有一个固定的例子:

<body>
    <table id="myTree" data-dojo-type="dojox/grid/TreeGrid" summary="This is a test">
        ...
    </table>
</body>
<script>
    require([
        "dojo/parser",
        "dijit/registry",
        "dojo/data/ItemFileReadStore",
        "dojox/grid/TreeGrid"
    ], function(parser, registry, ReadStore) {
        parser.parse();
        //var store = new ReadStore(...);
        var tree = registry.byId("myTree");
        console.log(tree);
        // tree.setStore( store );
    });
</script>