Sencha Touch2:在列表中的单独行中显示嵌套的Json数据

时间:2013-03-21 10:25:02

标签: javascript json listview extjs sencha-touch-2

我是sencha touch2的新手,在列表中的单独行中显示嵌套的json数据时遇到问题。 这是我的Json看起来像:

[
    {
        "work": {
            "agent": {
                "activeFlag": "false",
                "shiftId": "0",
                "id": "0",
                "deleteFlag": "false"
            },
            "id": "124",
            "status": "Unassigned",
            "assignment": {
                "pnr": {
                    "locator": "ABCDEF",
                    "connectTime": "0",
                    "id": "0"
                },
                "id": "123",
                "alerts": "Delay",
                "customers": [
                    {
                        "lastName": "XYZ",
                        "firstName": "MNO"
                    },
                    {
                        "lastName": "PQR",
                        "firstName": "STU "
                    }
                ]
            }
        }
    },
    {
        "work": {
            "agent": {
                "activeFlag": "false",
                "shiftId": "0",
                "id": "0",
                "deleteFlag": "false"
            },
            "id": "124",
            "status": "Unassigned",
            "assignment": {
                "pnr": {
                    "locator": "ABCDEF",
                    "connectTime": "0",
                    "id": "0"
                },
                "id": "123",
                "alerts": "Delay",
                "customers": [
                    {
                        "lastName": "ANY",
                        "firstName": "KLJ"
                    },
                    {
                        "lastName": "CHE",
                        "firstName": "MAK"
                    }
                ]
            }
        }
    }
]
像这样,我有30个'工作'对象,在1'工作'我有1'客户'阵列,我有多个客户

我想在列表中的单独行中显示“客户”,但我能够在一行中显示单个“工作”的所有客户,例如。

Output should be:
---------------
delay
First Name: MNO
---------------
delay
First Name: STU
---------------
delay
First Name: KLJ
---------------
delay
First Name: MAK
---------------

这里是型号。 ModelList.js:

Ext.define('CustomList.model.ModelList', {
    extend: 'Ext.data.Model',
    xtype:'modelList',
    requires:['CustomList.model.Customers'],
    config: {
            fields:['work'],
        proxy:{
            type:'ajax',
            url:'http://localhost:9091/CustomListJson/app/store/sample.json',
            reader:{
            type:'json'

        }
    },
    hasMany:{model:'CustomList.model.Customers',
             name:'customers'}
    }

});

Customers.js:

Ext.define('CustomList.model.Customers', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
            'firstName','lastName'
        ],
        belongsTo: "CustomList.model.ModelList"
    }

});

这是我的ShowList.js:

Ext.define('CustomList.view.ShowList',{
    extend:'Ext.Panel',
    xtype:'showList',
    config:{
        layout:'fit',
        styleHtmlContent:'true',
        styleHtmlCls:'showListCls',
        items:[
            {
                xtype:'list',
                id: 'listitems',
                store:'StoreList',
            itemTpl:[ '{work.assignment.alerts}<br>',
                '<tpl for="work.assignment.customers">',
                'firstName: {firstName}<br>',
                '</tpl>'
            ]
 // am able get the below values in list
//                itemTpl:'{work.assignment.alerts}'
//                itemTpl:'{work.assignment.pnr.locator}'
//                  itemTpl:'{work.agent.activeFlag}'
//                itemTpl: '<b>{firstName} {lastName}     </b><br>'+'pnr: '+ '{locator}  <br>' +
//                    'Alerts: '+'{alerts}' +'status: '+'{status} '
               }]


    }
});

这是我的StoreList.js:

Ext.define('CustomList.store.StoreList', {
    extend:'Ext.data.Store',
    requires:['Ext.data.reader.Json'],
    config:{
        model:'CustomList.model.ModelList',
        autoLoad:'true'

    }
});

任何人都可以帮助我。感谢。

1 个答案:

答案 0 :(得分:0)

这是你追求的吗?

download here

working screenshot

这是一个非常简单的模拟,但它可以帮助你,我认为你的模型联想是令人困惑的东西。

列表:

Ext.define('MyApp.view.MyList', {
    extend: 'Ext.dataview.List',

    config: {
        store: 'MyJsonStore',
        itemTpl: [
            '<div><div><h1>{work.assignment.alerts}</h1></div><tpl for="work.assignment.customers"><div>First Name: {firstName}</div></tpl></div>'
        ]
    }

});

存储

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

    config: {
        data: [
            {
                work: {
                    agent: {
                        activeFlag: 'false',
                        shiftId: '0',
                        id: '0',
                        deleteFlag: 'false'
                    },
                    id: '124',
                    status: 'Unassigned',
                    assignment: {
                        pnr: {
                            locator: 'ABCDEF',
                            connectTime: '0',
                            id: '0'
                        },
                        id: '123',
                        alerts: 'Delay',
                        customers: [
                            {
                                lastName: 'XYZ',
                                firstName: 'MNO'
                            },
                            {
                                lastName: 'PQR',
                                firstName: 'STU '
                            }
                        ]
                    }
                }
            },
            {
                work: {
                    agent: {
                        activeFlag: 'false',
                        shiftId: '0',
                        id: '0',
                        deleteFlag: 'false'
                    },
                    id: '124',
                    status: 'Unassigned',
                    assignment: {
                        pnr: {
                            locator: 'ABCDEF',
                            connectTime: '0',
                            id: '0'
                        },
                        id: '123',
                        alerts: 'Delay',
                        customers: [
                            {
                                lastName: 'ANY',
                                firstName: 'KLJ'
                            },
                            {
                                lastName: 'CHE',
                                firstName: 'MAK'
                            }
                        ]
                    }
                }
            }
        ],
        storeId: 'MyJsonStore',
        proxy: {
            type: 'ajax',
            reader: {
                type: 'json'
            }
        },
        fields: [
            {
                name: 'work'
            }
        ]
    }
});

如果你让配置像我一样工作,那么你可以逐步添加你的模型和关联,以及你的ajax加载,一直测试,这应该可以帮助你发现问题所在。

此外,当您使用JSON数据时,您可能需要考虑使用Json Lint之类的工具,您发布的原始JSON blob难以阅读且格式错误,所有这些都会使开发变得更加困难。