我创建了一个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(+)符号与叶节点一起显示?

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

最佳答案

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

如您所见,keep用于

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

这就是过滤树的方式。

我在模型中创建了一种方法mayHaveChildren。如果此方法返回true,则您有一个可扩展节点。如果返回false,则说明您的节点正常。有关详细信息,请参见http://dojotoolkit.org/reference-guide/1.10/dijit/tree/Model.htmlmayHaveChildren返回值基于商店查询。

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

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>

关于tree - Dijit树过滤和搜索不适用于ObjectStoreModel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31769402/

10-12 15:11