ExtJS 4网格:使用关联显示嵌套数据模型

4

我是ExtJS的新手,遇到了一个问题。我有一个深嵌套的JSON数据已经成功地通过多个关联模型加载到了存储器中。但是,在接下来的步骤中,我无法在简单的表格中显示这个数据,请帮忙。如何在表格中显示深层次JSON中的内容……以下是我的JSON数据:

{
        "success" : "true",
        "total":2,
        "user" : 
        {
            "id" : 12,
            "email" : "abc@gmail.com",          
            "course" : 
            {
                "name" : "BESE",        
                "institute" : 
                [{
                    "name" : "Engineering University",
                    "semester" :
                    {
                        "number":1,
                        "TCH" : 12,
                        "GPA" : 2.32,
                        "Marks":23.32,
                        "record" : [
                            {
                                "subject" : "Global Studies",
                                "CH":2,
                                "GP":4,
                                "Grade": "A+",
                                "Marks":99.1
                            },
                            {
                                "subject" : "Digital Logic Design",
                                "CH":4,
                                "GP":3.5,
                                "Grade": "B+",
                                "Marks":79.1
                            }
                        ]
                    }
                },
                {
                    "name" : "Another University",
                    "semester" :
                    {
                        "number":2,
                        "TCH" : 22,
                        "GPA" : 1.32,
                        "Marks":13.32,
                        "record" : [
                            {
                                "subject" : "C++",
                                "CH":2,
                                "GP":3,
                                "Grade": "C+",
                                "Marks":59.1
                            },
                            {
                                "subject" : "Engg Math",
                                "CH":4,
                                "GP":2.5,
                                "Grade": "C",
                                "Marks":39.1
                            }
                        ]
                    }
                }]
            }
        }
    }

以下是我的代码......

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id',         type: 'int'},
        {name: 'email',      type: 'string'},
    ],
    proxy: {
        type: 'ajax',
        url : 'getRecord.php',
        reader: {
            type: 'json',
            root: 'user'
        }
    },
    hasMany: { model: 'Course', name: 'course' ,associationKey: 'course'}

});

Ext.define('Course', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id',         type: 'int'},

        {name: 'name',      type: 'string'},
    ],

    belongsTo: 'User',
    hasMany: { model: 'Institute', name: 'institute' ,associationKey: 'institute'}

});

Ext.define('Institute', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'id',         type: 'int'},

        {name: 'name',      type: 'string'},
    ],

    belongsTo: 'Course',
    hasMany: { model: 'Semester', name: 'semester',associationKey: 'semester' }

});

Ext.define('Semester', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'number',     type: 'int'},
        {name: 'TCH',        type: 'float'},
        {name: 'GPA',        type: 'float'},                
        {name: 'Marks',      type: 'float'},
    ],

    belongsTo: 'Institute',
    hasMany: { model: 'Record', name: 'record',associationKey: 'record' }
});
Ext.define('Record', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'subject',    type: 'string'},
        {name: 'CH',         type: 'float'},
        {name: 'GP',         type: 'float'},    
        {name: 'Grade',      type: 'string'},                   
        {name: 'Marks',      type: 'float'},
    ],
    belongsTo: 'Semester',
});

Ext.require('Ext.data.*');
Ext.require('Ext.grid.*');


Ext.onReady(function(){
    Ext.QuickTips.init();
    var store = new Ext.data.Store({
        model: "User",

    });

    store.load({

    });

 Ext.create('Ext.grid.Panel', {
        renderTo: Ext.getBody(),
        store: store,
        width: 400,
        height: 200,
        title: 'Application Users',
        columns: [
            {
                text: 'Name',
                width: 100,
                sortable: false,
                hideable: false,
                dataIndex: 'email'
            },
            {
                text: 'Email Address',
                width: 150,
                dataIndex: 'course>aname',

            },
            {
                text: 'Phone Number',
                flex: 1,
                dataIndex: 'User.course.name'
            }
        ]
    });
});
3个回答

5

目前默认情况下,无法轻松地将嵌套数据加载到网格中,但正在积极开发解决此问题。


前Ext JS联合创始人和核心开发者 - zw324
为什么ExtJS不能像其他较小的项目一样,只需呈现整个JSON树? - Niklas Rosencrantz
积极参与开发,但到了2016年仍未得以继续。无能为力。 - hbulens

1
也许可以使用映射来帮助你,我以这种方式解决了类似但更简单的问题: 我的JSON:
{"operations": 
     [
        {"timestamp": {"__dt__": "2011-08-31T15:39:17"}, "description": "pippo"}, 
        {"timestamp": {"__dt__": "2011-08-31T16:51:43"}, "description": "pluto"}
     ]
}

我的模型:

Ext.define('Operation', {
    extend: 'Ext.data.Model',
    idProperty: 'timestamp.__dt__',
    fields: [{name:'__dt__', allowBlank:true, mapping:'timestamp.__dt__'},
             {name:'description', allowBlank:false}]
});

我的列:

columns: [{header: 'Timestamp', width: 150, sortable: true, dataIndex: '__dt__'},
          {header: 'Description' , width: 100, sortable: true, dataIndex: 'description', editor:'textfield'}],

希望有所帮助...


1
如果您的JSON对象不符合ExtJS的要求,只需更改对象格式即可。您可以执行类似以下操作:
Ext.ajax.request({url: 'getRecords.php', success: function(response) {
    var data = Ext.decode(response.reseponseText);
    //format your data
    grid.loadData(formatedData);
}})

你也可以在服务器端格式化你的 JSON 对象


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接