Dijit 树过滤和搜索不适用于 ObjectStoreModel

Dijit Tree filtering and search not working on ObjectStoreModel

我创建了一个 dijit 树和一个文本框,我想根据文本框中提供的关键字过滤树节点。我实施了另一个问题中提供的解决方案,但它似乎不起作用。当用户在文本框中输入某个单词时,树会重新填充相同的数据。

dijit.Tree search and refresh

以下是我的代码:

require(["dijit/form/TextBox","dojo/store/Memory","dijit/tree/ObjectStoreModel","dijit/Tree","dojo/domReady!"],     function(TextBox, MemoryStore, ObjectStoreModel, Tree) {

var searchBox = new TextBox({
    placeHolder: "[  Type here to search  ]"
}, "searchBox");

searchBox.on("blur", function() {
    tree.model.store.query({
        name: "*" + searchBox.value + "*"
    });

 /*the below approach has been taken from the other question*/
    tree.model.store.clearOnClose = true;
    /*tree.model.store.close(); //This is not working?*/
    delete tree._itemNodesMap;
    tree._itemNodesMap = {};
    tree.rootNode.state = "UNCHECKED";
    delete tree.model.root.children;
    tree.model.root.children = null;
    tree.rootNode.destroyRecursive();
    tree.model.constructor(tree.model)
    tree.postMixInProperties();
    tree._load();

});

var store = new MemoryStore({
    idProperty: "id",
    getChildren: function(object) {
        return this.query({
            parent: object.id
        });
    },
    data: [{
        id: "0",
        name: "Root Node",
        parent: null
    }, {
        id: "1",
        name: "File",
        parent: "0"
    }, {
        id: "2",
        name: "System",
        parent: "0"
    }, {
        id: "3",
        name: "Diagnosis",
        parent: "0"
    }, {
        id: "4",
        name: "Management",
        parent: "0"
    }]
});

var model = new ObjectStoreModel({
    store: store,
    query: {
        id: "0"
    }
});

var tree = new Tree({
    model: model,
    showRoot: false
}, "treeDiv");

tree.startup();

});

参见 JSFIDDLE 中的示例代码: http://jsfiddle.net/xLfdhnrf/16/

树和文本框呈现良好,只是搜索不起作用,有什么建议吗?还有为什么 EXPAND (+) 符号与叶节点一起显示?

我在模型数据中添加了一个自定义 属性。它被命名为keep,负责过滤。每一项数据都有这个属性。如果 keeptrue,该项目将可见。如果 keepfalse,该项目将被隐藏。
当输入模糊时,keep 更新并重新创建树。
为了保持树结构,如果一个项目与文本匹配,我们递归地将它的所有父级标记为 keep,即使它们不匹配(否则你将看不到项目本身)
我为重新创建树评论了一些不需要的行。

如您所见,keep用于

    getChildren: function(object) {
        return this.query({
            parent: object.id,
            keep: true
        });
    },

这就是过滤树的方式。

我在模型中创建了一个方法 mayHaveChildren。如果此方法 return true 你有一个可扩展的节点。如果它 returns false 你有一个正常的节点。有关详细信息,请参阅 http://dojotoolkit.org/reference-guide/1.10/dijit/tree/Model.html
mayHaveChildren return 值基于商店查询。

最后,我使用了正则表达式而不是纯字符串,因此匹配不区分大小写。

require(["dijit/form/TextBox", "dojo/store/Memory", "dijit/tree/ObjectStoreModel", "dijit/Tree", "dojo/domReady!"], function(TextBox, MemoryStore, ObjectStoreModel, Tree) {
    var searchBox = new TextBox({
        placeHolder: "[  Type here to search  ]"
    }, "searchBox");
    searchBox.on("blur", function() {
        var includeParent = function(itemId) {
          tree.model.store.query({
            id: itemId
          }).forEach(function(item) {
            item.keep = true;
            //and we include all parent tree
            includeParent(item.parent);
        });
        }
      
        //reset all node, first we exlude them all
        tree.model.store.query().forEach(function(item) {
          item.keep = false;
        });
        //then we include only the one matching
        tree.model.store.query({
           name: new RegExp('.*' + searchBox.value + '.*', 'i')
        }).forEach(function(item) {
          item.keep = true;
          //and we include all parent tree
          includeParent(item.parent);
        });
        

        //delete tree._itemNodesMap;
        //tree._itemNodesMap = {};
        //tree.rootNode.state = "UNCHECKED";
        //delete tree.model.root.children;
        //tree.model.root.children = null;
        tree.rootNode.destroyRecursive();
        tree.model.constructor(tree.model)
        tree.postMixInProperties();
        tree._load();
    });

    var store = new MemoryStore({
        idProperty: "id",
        getChildren: function(object) {
            return this.query({
                parent: object.id,
                keep: true
            });
        },
        data: [{
            id: "0",
            name: "Root Node",
            parent: null,
            keep: true
        }, {
            id: "1",
            name: "File",
            parent: "0",
            keep: true
        }, {
            id: "2",
            name: "System",
            parent: "0",
            keep: true
        }, {
            id: "3",
            name: "Diagnosis",
            parent: "0",
            keep: true
        }, {
            id: "4",
            name: "Management",
            parent: "0",
            keep: true
        },
        {
            id: "5",
            name: "New",
            parent: "1",
            keep: true
        },
        {
            id: "6",
            name: "Open",
            parent: "1",
            keep: true
        },
        {
            id: "7",
            name: "Import",
            parent: "1",
            keep: true
        }]
    });

    var model = new ObjectStoreModel({
        store: store,
        query: {
            id: "0"
        },
        mayHaveChildren: function (item) { return store.query({parent: item.id}).length > 0; }
    });

    var tree = new Tree({
        model: model,
        showRoot: false
    }, "treeDiv");

    tree.startup();

});
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<link rel="stylesheet" type="text/css" href="//ajax.googleapis.com/ajax/libs/dojo/1.10.0/dijit/themes/claro/claro.css">


<body class="claro">
<div id="searchBox"></div>
<div id="treeDiv"></div>        
</div>