0

我是 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
---------------

这是模型。模型列表.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'}
    }

});

客户.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'

    }
});

谁能帮帮我吗。谢谢。

4

1 回答 1

0

这就是你所追求的吗?

在这里下载

工作截图

这是一个非常简单的模型,但它应该可以帮助您,我认为您的模型关联令人困惑。

列表:

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 难以阅读且格式错误,所有这些都使开发变得更加困难。

于 2013-03-26T17:06:40.553 回答