EXT Js 复杂 JSON 响应处理:网格插入
您好,我正在尝试动态地将 JSON 记录插入到网格中。 我的服务器响应如下:
{
"studentDetails":{
"status":"ACTIVE",
"subject":"MATH",
"paymentOptions":"EFT",
"idStudent":71,
"firstName":"Alli",
"lastName":"Alli",
"middleName":"Alli",
"grade":"A",
"kumonLevel":"FK",
"parentId":68,
"userId":1,
"parentBean":{
"parentFirstName":"Alli",
"idParent":68,
"parentMiddleName":"Alli",
"parentLastName":"Alli",
"parentEmailId":"[email protected]",
"parentPhoneNumber":"3173797945"},
"startTimeSlot":"12:00 AM",
"endTimeSlot":"12:15 AM",
"dob":"2012-07-04"},
"success":true
}
一名学生在上述响应中将parentDetails 作为单独的对象。
在我的客户那里我做了以下事情: 我有一个具有以下配置的网格:
var studentRecord = Ext.data.Record.create( [ {
name : 'firstName',
type : 'string'
}, {
name : 'lastName',
type : 'string'
}, {
name : 'middleName',
type : 'string'
}, {
name : 'grade',
type : 'string'
}, {
name : 'kumonLevel',
type : 'string'
}, {
name : 'startTimeSlot',
type : 'string'
}, {
name : 'endTimeSlot',
type : 'string'
}, {
name : 'subject',
type : 'string'
}, {
name : 'dob',
type : 'date'
}, {
name : 'status',
type : 'string'
}, {
name : 'paymentOptions',
type : 'string'
}, {
name : 'parentFirstName',
mapping : 'parentBean.parentFirstName',
type : 'string'
}, {
name : 'parentLastName',
mapping : "parentBean['parentLastName']",
type : 'string'
}, {
name : 'parentMiddleName',
mapping : 'parentBean.parentMiddleName',
type : 'string'
}, {
name : 'parentPhoneNumber',
mapping : 'parentBean.parentPhoneNumber',
type : 'String'
}, {
name : 'parentEmailId',
mapping : 'parentBean.parentEmailId',
type : 'string'
} ]);
var myProxy = new Ext.data.HttpProxy( {
method : 'GET',
url : 'listActiveStudents.do'
});
var studentsListReader = new Ext.data.JsonReader( {
successProperty : 'success',
root : 'studentDetails',
idProperty : 'idStudent'
}, studentRecord);
var studentDS = new Ext.data.Store( {
proxy : myProxy,
autoLoad : true,
totalProperty : 'total',
reader : studentsListReader
});
从服务器接收数据后,我执行了以下操作:
handler : function() {
studentForm.getForm().submit({
url : 'createStudent.do',
waitMsg : 'Saving Data...',
submitEmptyText : false,
success : function(form, action) {
win.close();
var studentDetail = action.result.studentDetails;
var xyz = new studentDS.recordType(studentDetail, 0);
studentDS.insert(0,xyz);
}
});
}
我在这里面临的问题是:
插入记录时,parentDetails 不会显示在网格中。插入时映射似乎无法正常工作。最初加载网格时,它呈现完美。我创建了很好的记录,它也在 Firefox 调试器中显示了有效值。
有人可以指导我吗?
Hello I am trying to insert a JSON record into the grid dynamically.
My server response is as below :
{
"studentDetails":{
"status":"ACTIVE",
"subject":"MATH",
"paymentOptions":"EFT",
"idStudent":71,
"firstName":"Alli",
"lastName":"Alli",
"middleName":"Alli",
"grade":"A",
"kumonLevel":"FK",
"parentId":68,
"userId":1,
"parentBean":{
"parentFirstName":"Alli",
"idParent":68,
"parentMiddleName":"Alli",
"parentLastName":"Alli",
"parentEmailId":"[email protected]",
"parentPhoneNumber":"3173797945"},
"startTimeSlot":"12:00 AM",
"endTimeSlot":"12:15 AM",
"dob":"2012-07-04"},
"success":true
}
A student having parentDetails as a seperate object in the above response.
at my client I did the following :
i had a grid with the below configuration :
var studentRecord = Ext.data.Record.create( [ {
name : 'firstName',
type : 'string'
}, {
name : 'lastName',
type : 'string'
}, {
name : 'middleName',
type : 'string'
}, {
name : 'grade',
type : 'string'
}, {
name : 'kumonLevel',
type : 'string'
}, {
name : 'startTimeSlot',
type : 'string'
}, {
name : 'endTimeSlot',
type : 'string'
}, {
name : 'subject',
type : 'string'
}, {
name : 'dob',
type : 'date'
}, {
name : 'status',
type : 'string'
}, {
name : 'paymentOptions',
type : 'string'
}, {
name : 'parentFirstName',
mapping : 'parentBean.parentFirstName',
type : 'string'
}, {
name : 'parentLastName',
mapping : "parentBean['parentLastName']",
type : 'string'
}, {
name : 'parentMiddleName',
mapping : 'parentBean.parentMiddleName',
type : 'string'
}, {
name : 'parentPhoneNumber',
mapping : 'parentBean.parentPhoneNumber',
type : 'String'
}, {
name : 'parentEmailId',
mapping : 'parentBean.parentEmailId',
type : 'string'
} ]);
var myProxy = new Ext.data.HttpProxy( {
method : 'GET',
url : 'listActiveStudents.do'
});
var studentsListReader = new Ext.data.JsonReader( {
successProperty : 'success',
root : 'studentDetails',
idProperty : 'idStudent'
}, studentRecord);
var studentDS = new Ext.data.Store( {
proxy : myProxy,
autoLoad : true,
totalProperty : 'total',
reader : studentsListReader
});
upon data receiving from the server I did the following :
handler : function() {
studentForm.getForm().submit({
url : 'createStudent.do',
waitMsg : 'Saving Data...',
submitEmptyText : false,
success : function(form, action) {
win.close();
var studentDetail = action.result.studentDetails;
var xyz = new studentDS.recordType(studentDetail, 0);
studentDS.insert(0,xyz);
}
});
}
The problem what I am facing here is :
When the record is inserted the parentDetails are not showing up in the grid. Seems, the mapping is not working properly while inserting. Where as while loading the grid initially it renders perfectly. I created the record well which shows in firefox debbuger with valid values too.
could any one guide me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个非常棘手的问题。但我找到了一个简单(但肮脏)的解决方案,您只需编辑创建记录的行,从这个:
到这个:
这就是原因
所以在跟踪调用堆栈之后,我发现您从
Store
加载的记录与通过新的 StudentDS.recordType
创建的记录之间存在有趣的差异。看看差异:
事实上,当您加载数据时,您的
JsonStore
和JsonReader
对您加载的数据进行了肮脏的黑客攻击。他们线性化您的数据。它们将所有数据从parentBean
移动到record.data
的根,因此在渲染网格时,可以正确渲染网格。要了解它们如何呈现网格,请检查 GridView.js 第 827 行处的代码,
注意 GridView 如何捕获值。他们使用
record.data[column.name]
来获取每个字段的值,这是所有parent*
报告未定义的地方。因此,要了解他们如何线性化您的代码,您可以检查 DataReader.js 的第 157 行。
事实上,他们在这里使用的
new Record
与您的new 相同StudentDS.recordType
,但他们代表您extractValues
,并且显然您没有这样做,因为这样做并不明显。知道了?希望解释足够清楚。
This is a very tricky issue. but I found an easy (but dirty) solution, which you just need to edit the line where you create your record, from this:
to this:
And here is why
So after tracing the calling stack, I found an interesting differences between the records you loaded from
Store
, and the record that you created through yournew studentDS.recordType
.Look at the differences:
In fact, while you load your data, your
JsonStore
andJsonReader
did a dirty hack to your loaded data. They linearize your data. They move all your data from yourparentBean
to the root of therecord.data
, so while rendering the grid, your grid can be rendered correctly.To know how they render your grid, check the code at Line 827 of GridView.js
Notice how the GridView is capturing the values. They use
record.data[column.name]
to get the value of each fields, and this is where all yourparent*
report undefined.And so to know how they linearize your codes, you can check line 157 of DataReader.js
And in fact the
new Record
they used here is identical with yournew studentDS.recordType
, with the exception that theyextractValues
for you on behalf, and clearly you did not do it because it's not obvious to do so.Got it? Hope the explanation is clear enough.