dojo如何获取特定类型的所有树节点?

时间:2010-10-11 06:27:27

标签: dojo

我编写了以下代码来创建一个dojo树。

store = new dojo.data.ItemFileWriteStore({url: link});
                              treeModel = new dijit.tree.TreeStoreModel({
                                            store: store,
                                            query: {
                                                    "type": "ROOT"
                                                    },
                                            rootId: "newRoot",
                                            childrenAttrs: ["children"]
                                        });

                              tree= new dijit.Tree({model: treeModel},"treeOne");

以下是我的JSON文件结构:

{
   identifier: "id",
   label: "name",
   items: [
   {id: "ROOT",name: "Change Windows",type: "ROOT"},
   ]}

我想得到特定'type'的所有节点(基本上都是'id'部分),比如说type =“ROOT”。反正有没有获得所有这些节点?我想过使用tree._itemNodeMap来做这件事,但是不知道如何迭代这个整个项目映射,因为它需要一个id作为输入来返回任何特定的节点。

1 个答案:

答案 0 :(得分:4)

如果您正在讨论以编程方式获取数据项,则可以使用fetch直接从商店获取数据项。

ItemFile * Store的样本JSON:

{
    "identifier": "id",
    "label": "name",
    "items": [{
        "id": "ROOT",
        "name": "Root",
        "type": "ROOT",
        "children": [{
            "id": "P1",
            "name": "StackExchange",
            "type": "website",
            "children": [{
                "id": "C1",
                "name": "StackOverflow",
                "type": "website"
            },
            {
                "id": "C2",
                "name": "ServerFault",
                "type": "website"
            }]
        },
        {
            "id": "P2",
            "name": "Sandwich",
            "type": "food",
            "children": [{
                "id": "C3",
                "name": "Ham",
                "type": "food"
            },
            {
                "id": "C4",
                "name": "Cheese",
                "type": "food"
            }]
        },
        {
            "id": "P3",
            "name": "Potluck",
            "type": "mixed",
            "children": [{
                "id": "C5",
                "name": "Google",
                "type": "website"
            },
            {
                "id": "C6",
                "name": "Banana",
                "type": "food"
            }]
        }]
    }]
}

示例代码:

dojo.require('dojo.data.ItemFileReadStore');

dojo.ready(function() {
    var store = new dojo.data.ItemFileReadStore({
        url: 'so-data.json'
    });
    store.fetch({
        query: {
            type: 'food'
        },
        queryOptions: {
            deep: true
        },
        onItem: function(item) {
            console.log(store.getLabel(item));
        }
    });
});

这将记录三明治,火腿,奶酪和香蕉。

相关问题