简体   繁体   English

如何在extjs4中创建动态treePanel

[英]How to create dynamic treePanel in extjs4

i am working in extjs4. 我在extjs4中工作。 i have view with tree panel as- 我有树面板的视图-

Ext.define('Balaee.view.qb.qbquestion.tree1', {

    extend: 'Ext.tree.Panel',
    title: 'Simple Tree',
    width: 200,
    height: 150,
    alias : 'widget.tree1',
   //store: 'qb.qbquestioncomplexityStore',
    rootVisible: true,
    renderTo: Ext.getBody()
});

In controller i have created static store. 在控制器中,我创建了静态存储。 And binded it to this view. 并将其绑定到此视图。 Code is as- 代码为-

 var store = Ext.create('Ext.data.TreeStore', {
            root: {
                expanded: true,
                children: [
                    { text: "detention", leaf: true },
                    { text: "homework", expanded: true, children: [
                        { text: "book report", leaf: true },
                        { text: "algebra", leaf: true}
                    ] },
                    { text: "buy lottery tickets", leaf: true }
                ]
            }
        });

     var bchart=Ext.create('Balaee.view.qb.qbquestion.tree1',{
         store:store

     });
     var comp=Ext.getCmp('QuestionView');
     comp.removeAll();
     comp.add(bchart);

its working correctly. 其正常工作。 Actually its static store. 实际上是静态存储。 i want to create tree panel for json- 我想为json创建树面板

{"data":[{"Maincategory":"Main","maincategoryId":"1","Subcategory":{"subcategory":"GK","categoryId":"2"},{"subcategory":"History","categoryId":"3"},{"subcategory":"Geography","categoryId":"4"}]},{"Maincategory":"GK","maincategoryId":"2","Subcategory":[{"subcategory":"environment","categoryId":"5"}]},{"Maincategory":"History","maincategoryId":"3","Subcategory":[{"subcategory":"civics","categoryId":"7"}]},{"Maincategory":"Geography","maincategoryId":"4","Subcategory":[{"subcategory":"India","categoryId":"8"}]},{"Maincategory":"environment","maincategoryId":"5","Subcategory":[]}]}

So what modifications i need to do in this json? 那么我需要在这个json中做哪些修改? Do i need to change store? 我需要换店吗? Please can someone please guide me 请有人可以指导我

If you can change your JSON, yes, you definitely need to change the data. 如果可以更改JSON,是的,您肯定需要更改数据。 First of all, you have no common fields throughout your data. 首先,整个数据中没有公共字段。 Your JSON should probably look more like example: 您的JSON应该看起来更像示例:

{
    "data": [{
            "category": "Main",
            "categoryId": "1",
            "subcategory": [{
                    "category": "GK",
                    "categoryId": "2"
                },{
                    "category": "History",
                    "categoryId": "3"
                },{
                    "category": "Geography",
                    "categoryId": "4"
            }]
        },{
            "category": "GK",
            "categoryId": "2",
            "subcategory": [{
                    "category": "environment",
                    "categoryId": "5"
            }]
    }, .... ]
}

Ext.define('SomeModel', {
extend: 'Ext.data.Model',
fields: [
       { name: 'category'},
       { name: 'categoryId', type: 'int'}
    ]
});


// Your tree store should look like this based on the data

Ext.define('SomeTreeStore', {
    extend: 'Ext.data.TreeStore',
    model: 'SomeModel', // defines your JSON fields 
    defaultRootProperty: 'Subcategory', // your children property name or rename 
                                        // this field in your JSON to children 
                                        // and you don't need this
    proxy: {
        type: 'ajax',
        url: '/yourjsonurl',
        // don't need this section if you define your JSON the way the tree store wants it
        reader: {
            type: 'json',
            root: 'data'
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM