How to add data in json format to TreeStore in ExtJS

Source: Internet
Author: User
Ext indicates that there are two ways to bind data to treeStore. One is the root attribute. The code is as follows: root: {expanded: true, text: & quot; myRoot & quot;, children: [{text: & quot; Child1

Ext indicates that there are two ways to bind data to treeStore. One isRootThe code is as follows:

root: {    expanded: true,    text: "My Root",    children: [        { text: "Child 1", leaf: true },        { text: "Child 2", expanded: true, children: [            { text: "GrandChild", leaf: true }        ] }    ]}

In this way, only data in Tree format can be added, but if you want to add data in TreeGrid format, it will not work, so Pass.

The other isProxyMethod: there are two options for this method: Client and Server. Now I want to bind the data in json format. I must select Client, there are also three methods for the Client, of which only MemoryProxy is irrelevant to the browser, so select this method.

The Code is as follows:

//this is the model we will be using in the storeExt.define('User', {    extend: 'Ext.data.Model',    fields: [        {name: 'id',    type: 'int'},        {name: 'name',  type: 'string'},        {name: 'phone', type: 'string', mapping: 'phoneNumber'}    ]});//this data does not line up to our model fields - the phone field is called phoneNumbervar 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 abovevar store = Ext.create('Ext.data.Store', {    autoLoad: true,    model: 'User',    data : data,    proxy: {        type: 'memory',        reader: {            type: 'json',            root: 'users'        }    }});

If you use this code for testing, you cannot pass it because you add the store to the tree. an error will be reported in the panel, because the store format you created is not in TreeStore format. You need to modify this field and get the data to the proxy, because TreeStore does not have the data attribute, this is a problem with the Ext sample code. The modified result is:

var store = Ext.create('Ext.data.TreeStore', {autoLoad: true,model: 'Case',proxy: {type: 'memory',data: data,reader: {type: 'json',root: 'users'}}});


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.