Unable to create ExtJS tree from json

拟墨画扇 提交于 2019-12-13 04:26:01

问题


I am trying to create a tree from json file. The JSON data is:

[
    {
        "root": {
            "text": "Root Node",
            "expanded": true,
            "children": [
                {
                    "text": "Invisible",
                    "leaf": true,
                    "children": [
                        {
                            "text": "Bookmark 2",
                            "leaf": true
                        },
                        {
                            "text": "Bookmark 3",
                            "leaf": true
                        }
                    ]
                },
                {
                    "text": "Visible",
                    "leaf": true,
                    "children": [
                        {
                            "text": "Bookmark 4",
                            "leaf": true
                        },
                        {
                            "text": "Bookmark 5",
                            "leaf": true
                        }
                    ]
                }
            ]
        }
    }
]

Here is the code I am using for my store:

Ext.define('DHT.store.Categories', {
    extend: 'Ext.data.TreeStore',
    model: 'DHT.model.Category',
    autoLoad: true,
    autoSync: true,
    proxy: {
        type: 'ajax',        
        url: 'treedata.json',
        reader:
        {
            type: 'json'           
        }
    }
});

and here is the code for tree:

Ext.define('DHT.view.Category.CategoryList', {   
    extend: 'Ext.tree.Panel',
    alias: 'widget.treeList',
    width: 200,
    height: 400,
    store: Ext.create('DHT.store.Categories'),
    rootVisible: false
});

The above code is only showing folder image that keep on expanding! Can someone point out the problem?


回答1:


  1. You have leaf: true and children, this is not possible

        {
            "text": "Invisible",
            "leaf": true,
            "children": [
                {
                    "text": "Bookmark 2",
                    "leaf": true
                },
                ...
            ]
        }
    

    correct:

    {
        "text": "Invisible",
        "leaf": false,
        "children": [
            {
                "text": "Bookmark 2",
                "leaf": true
            },
            ...
        ]
    }
    
  2. You have to define a root property in your treestore

  3. The root property needs to be consistent, in your case it would be 'root', 'children', 'children'
    Read this: Treepanel with nested data from JSON



来源:https://stackoverflow.com/questions/20611324/unable-to-create-extjs-tree-from-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!