Laoding JSON data into Ext.data.Tree store on click on a button

754 views Asked by At

I have created a tree panel in ExtJs 4.1 and that panel is associated with store. I have some JSON data available in a variable. How can I load that into into tree store on click of a button.

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id',    type: 'int'},
        {name: 'name',  type: 'string'},
        {name: 'phone', type: 'string', mapping: 'phoneNumber'}
    ]
});


var data = {
    users: [
        {
            id: 1,
            name: 'Ed Spencer',
            phoneNumber: '555 1234'
        },
        {
            id: 2,
            name: 'Abe Elias',
            phoneNumber: '666 1234'
        }
    ]
};

//note how we set the 'root' in the reader to match the data structure above

var store = Ext.create('Ext.data.TreeStore', {
    autoLoad: false,
    model: 'User',
    proxy: {
        type: 'memory',

    }
});

How can I load data on click of a button?

3

There are 3 answers

1
Vishal Zanzrukia On

you can use store.loadData method on buttons click. Check doc here.

0
Jack.W On

Try store.data = data, It appears that there is no methods like 'setData', just try store.data = data maybe it will work

0
Shlomo On
var store = Ext.create('Ext.data.TreeStore', {
    model: yourModel, // make sure the model fits
    proxy: {
        data : yourData, // this is your var with json
        type: 'memory',
        reader: {
            type: 'json'
        }
    },
});

Check out the answers over here: Load static data to Ext.data.TreeStore

To run your code on a button click, do:

Ext.create('Ext.Button', {
    text: 'Click me',
    handler: function() {
        // load the store
    }
});