Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

extjs4 - Unable to create ExtJS tree from json

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...