Search code examples
jsonextjstreelocaltreepanel

extjs treepanel filled with local data


I want to setup a treepanel with local data like this:

data =
{"root":
    {
     "08": {"field": "getCategory3()","name": "Untergruppe","object": "product"},
     "09": {"field": "getCategory2()","name": "Obergruppe","object": "product"}
    }
};

but im confused about how to get the data out of this string.

var mystore = Ext.create('Ext.data.TreeStore', {
model: treemodel,
proxy: {
    data : data, 
    type: 'memory',
    reader: { type: 'json' }
},
});

How to create the model for this? Right now, no data is showing up in my tree

var treemodel = Ext.define('Task', {
extend: 'Ext.data.Model',
fields: [
    {name: 'name', type: 'string'},
    {name: 'object', type: 'string'},
]
});

var myTree = Ext.create('Ext.tree.Panel',
{   xtype: 'treepanel', id: 'myTree',
    containerScroll: false,
    store: mystore,
    root: 'root',
    margin: '0 15 0 0',
    viewConfig: {
        plugins: {
            ptype: 'treeviewdragdrop',
            appendOnly: true,
            sortOnDrop: true,
            containerScroll: true
        }
    }
}
); 

Solution

  • pretty simple:

    var txt = 'root: {
        expanded: true,
        children: [{
            "id": "product",
            text: "product",
            "name": "product",
            leaf: false,
            children: [{
                "id": "08",
                text: "Untergruppe",
                "name": "getCategory3()",
                leaf: true
            }, {
                "id": "09",
                text: "Obergruppe",
                "name": "getCategory2()",
                leaf: true
            }, ]
        }, {
            "id": "shopproduct",
            text: "shopproduct",
            "name": "shopproduct",
            leaf: false,            
            children: [{
                "id": "04",
                text: "Lagerbestand",
                "name": "available_value",
                leaf: true
            }, {
                "id": "10",
                text: "Ust",
                "name": "getVAT()",
                leaf: true
            }]
        }, ]
    }';
    
    var data = Ext.decode(txt);
    
    var store = Ext.create('Ext.data.TreeStore', {
        root: data
    });
        Ext.create('Ext.tree.Panel', {
        title: 'Simple Tree',
        width: 500,
        height: 500,
        store: store,
        rootVisible: false,
        renderTo: Ext.getBody()
    });