extjs parsing nested json in template

≯℡__Kan透↙ 提交于 2019-12-05 09:04:00

I think I got it (hopefully it's correct).

So, create a field for each nested group of data you need. So I have a Contacts model. In that model there are these fields:

id
client_id
addresses //mapped to addresses
address //mapped to addresses.address 

then in the template:

<br>
<tpl for="addresses">
    id: {id}<br>
    addy id: {address_id}<br>

    <tpl for="address">
        {city} {state}, {zip}<br>
    </tpl>

</tpl>

This is what the whole thing looks like:

View

Ext.define('MyApp.view.MyView', {
    extend: 'Ext.view.View',

    height: 250,
    width: 400,
    itemSelector: 'div',
    store: 'MyJsonStore',

    initComponent: function() {
        var me = this;

        Ext.applyIf(me, {
            itemTpl: [
                '<br>',
                '<tpl for="addresses">',
                '    id: {id}<br>',
                '    addy id: {address_id}<br>',
                '    <b>',
                '    <tpl for="address">',
                '        {city} {state}, {zip}<br><br>',
                '    </tpl>',
                '    </b>',
                '',
                '</tpl>',
                '',
                '<hr>',
                ''
            ]
        });

        me.callParent(arguments);
    }

});

Store

Ext.define('MyApp.store.MyJsonStore', {
    extend: 'Ext.data.Store',

    requires: [
        'MyApp.model.Contacts'
    ],

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        me.callParent([Ext.apply({
            storeId: 'MyJsonStore',
            model: 'MyApp.model.Contacts',
            data: {
                contacts: [
                    {
                        id: '1',
                        client_id: '135468714603',
                        addresses: [
                            {
                                id: '1',
                                contact_id: '1',
                                address_id: '16',
                                address: {
                                    '0': {
                                        id: '16',
                                        address: '123 Some Rd',
                                        address2: '',
                                        city: 'Toen',
                                        state: 'VS',
                                        zip_code: '11111',
                                        country: 'USA'
                                    }
                                }
                            },
                            {
                                id: '6',
                                contact_id: '1',
                                address_id: '26',
                                address: {
                                    id: '26',
                                    address: '1 Other Road',
                                    address2: '',
                                    city: 'Twn',
                                    state: 'BD',
                                    zip_code: '11112',
                                    country: 'USA'
                                }
                            }
                        ]
                    },
                    {
                        id: '10',
                        client_id: null,
                        addresses: [
                            {
                                id: '8',
                                contact_id: '10',
                                address_id: '28',
                                address: {
                                    id: '28',
                                    address: '54 Road',
                                    address2: '',
                                    city: 'TWND',
                                    state: 'TT',
                                    zip_code: '11113',
                                    country: 'USA'
                                }
                            },
                            {
                                id: '9',
                                contact_id: '10',
                                address_id: '29',
                                is_mailing_address: '0',
                                is_primary_address: '0',
                                display_priority: '0',
                                address: {
                                    id: '29',
                                    address: '6 Road',
                                    address2: '',
                                    city: 'TOEOEOWN',
                                    state: 'PY',
                                    zip_code: '11116',
                                    country: 'USA'
                                }
                            },
                            {
                                id: '10',
                                contact_id: '10',
                                address_id: '30',
                                address: {
                                    id: '30',
                                    address: 'PO Box 9',
                                    address2: '',
                                    city: 'TOYN',
                                    state: 'GF',
                                    zip_code: '11118',
                                    country: 'USA'
                                }
                            }
                        ]
                    },
                    {
                        id: '11',
                        client_id: null,
                        contact_id: '11',
                        addresses: [
                            {
                                id: '11',
                                contact_id: '11',
                                address_id: '33',
                                is_mailing_address: '0',
                                is_primary_address: '0',
                                display_priority: '0',
                                address: {
                                    id: '33',
                                    address: '4 Street',
                                    address2: '',
                                    city: 'TEOIN',
                                    state: 'TG',
                                    zip_code: '11119',
                                    country: 'USA'
                                }
                            }
                        ]
                    }
                ]
            },
            proxy: {
                type: 'memory',
                reader: {
                    type: 'json',
                    root: 'contacts'
                }
            }
        }, cfg)]);
    }
});

Model

Ext.define('MyApp.model.Contacts', {
    extend: 'Ext.data.Model',

    fields: [
        {
            name: 'id'
        },
        {
            name: 'client_id'
        },
        {
            name: 'addresses',
            mapping: 'addresses'
        },
        {
            name: 'address',
            mapping: 'address'
        }
    ]
});

I have verified that the above answer does work, but to note for future people, that if you don't specify the name of sub fields, you don't need the 2nd nested template. You can do it with just the first.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!