js中相关定义如下:
[code="js"][/code]
//form中 items项定义
items : [ {
xtype : 'hidden',
name : 'userId'
}, {
xtype : 'hidden',
name : 'userName'
}, {
xtype : 'textfield',
fieldLabel : '门店名称',
name : 'storeName',
allowBlank : 'false',
width : '200'
}, {
xtype : 'textarea',
fieldLabel : '门店描述',
name : 'description',
allowBlank : 'true',
width : '200'
} ]
var userRecord = Ext.data.Record.create([ {
name : 'userId'
}, {
name : 'storeName'
}, {
name : 'password'
}, {
name : 'description'
} ]);
function loadData() {
updateStoreForm.form.reader = new Ext.data.JsonReader({
totalProperty : 'totalCount',
root : 'result'
}, userRecord);
updateStoreForm.form.load({
waitMsg : '请稍后...',
waitTitle : '提示',
url : 'loginUser.do'
});
}
后台JSON数据:
{"totalCount":1,"result":{"description":"","password":"admin","storeName":"管理员","userId":"admin"}}
这样子为什么form加载不出JSON数据呢?
查看 Ext.form.Action.Submit api以及源码
[code="java"]/**
A class which handles loading of data from a server into the Fields of an {@link Ext.form.BasicForm}.
Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
{@link Ext.form.BasicForm#load load}ing.
Response Packet Criteria
A response packet must contain:
success property : Boolean
data property : Object
The data property contains the values of Fields to load.
The individual value object for each Field is passed to the Field's
{@link Ext.form.Field#setValue setValue} method.
JSON Packets
By default, response packets are assumed to be JSON, so for the following form load call:
var myFormPanel = new Ext.form.FormPanel({
title: 'Client and routing info',
items: [{
fieldLabel: 'Client',
name: 'clientName'
}, {
fieldLabel: 'Port of loading',
name: 'portOfLoading'
}, {
fieldLabel: 'Port of discharge',
name: 'portOfDischarge'
}]
});
myFormPanel.{@link Ext.form.FormPanel#getForm getForm}().{@link Ext.form.BasicForm#load load}({
url: '/getRoutingInfo.php',
params: {
consignmentRef: myConsignmentRef
},
failure: function(form, action) {
Ext.Msg.alert("Load failed", action.result.errorMessage);
}
});
{
success: true,
data: {
clientName: "Fred. Olsen Lines",
portOfLoading: "FXT",
portOfDischarge: "OSL"
}
}
{
success: false,
errorMessage: "Consignment reference not found"
}
[/code]
以及 源码中:
[code="java"]Ext.extend(Ext.form.Action.Load, Ext.form.Action, {
// private
type : 'load',
// private
run : function(){
Ext.Ajax.request(Ext.apply(
this.createCallback(this.options), {
method:this.getMethod(),
url:this.getUrl(false),
headers: this.options.headers,
params:this.getParams()
}));
},
// private
success : function(response){
var result = this.processResponse(response);
if(result === true || !result.success || !result.data){
this.failureType = Ext.form.Action.LOAD_FAILURE;
this.form.afterAction(this, false);
return;
}
this.form.clearInvalid();
this.form.setValues(result.data);//这里可以看出取得是data
this.form.afterAction(this, true);
},
.....
[/code]
json格式不对
[{"description":"","password":"admin","storeName":"管理员","userId":"admin"}]
非后台数据格式问题
采用form的load方式加载数据表单控件必须用id,所以他在搜索控件时 搜不到,所以设置不上值 。
但是有办法解决不设置id属性值 解决办法参考:http://man1900.iteye.com/blog/773769
var userRecord = Ext.data.Record.create([ {
name : 'userId', type: 'string
}, {
name : 'storeName', type: 'string
}, {
name : 'password', type: 'string
}, {
name : 'description', type: 'string
} ]);
{"totalCount":1,"result":[{"description":"","password":"admin","storeName":"管理员","userId":"admin"}]}
function loadData() {
updateStoreForm.form.reader = new Ext.data.JsonReader({
totalProperty : 'totalCount',
root : 'result'
}, userRecord);
updateStoreForm.form.load({
waitMsg : '请稍后...',
waitTitle : '提示',
url : 'loginUser.do'
});
}
因为你这样写的
totalProperty : 'totalCount',
root : 'result'定
所以后台返回应该这样
{"totalCount":1,"result":[{"description":"","password":"admin","storeName":"管理员","userId":"admin"}]}
Ext.form.Action.Load的 API文档开头就说明了响应数据包必须类似下面的格式:
{ success: true, data: { clientName: "Fred. Olsen Lines", portOfLoading: "FXT", portOfDischarge: "OSL" } }