我是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'
}
]
});
});
答案 0 :(得分:5)
目前的一个限制是,默认情况下嵌套数据不容易加载到网格中,但它正在积极处理。
答案 1 :(得分: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'}],
希望它有所帮助...
答案 2 :(得分: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对象