Ext.grid.EditorGridPanel保存

时间:2024-06-28 13:04:08

用get方法传递编辑的数据会出现乱码,解决get乱码的方法就是encodeURI(param),然后在后台转码:

String strJson =  new String(request.getParameter("param").getBytes("iso-8859-1"),"utf-8");  
        java.net.URLDecoder.decode(strJson, "UTF-8");

下面是get方法传参代码:

1、获取Ext.grid.EditorGridPanel的bbar添加保存按钮

 bbar:new Ext.PagingToolbar({
emptyMsg:"没有数据",
displayInfo:true,
displayMsg:"目前显示第 {0} - {1} 条,共 {2} 条",
store:store,
pageSize:20,
refreshText:"刷新列表",
items:['-', {
text:'保存',
handler:function(){
var m = store.modified.slice(-1);
var jsonArray = []; //定义修改后的JSON对象
Ext.each(m, function (item) {
jsonArray.push(item.data);
});
var strJson = Ext.encode(jsonArray);
if (jsonArray.length == 0) {
Ext.Msg.alert('提示', '没有对数据进行任何更改!');
return;
}else{
Ext.Msg.alert('提示', '保存!');
Ext.Ajax.request({
url: '/s.do/servlet?tg=s&json='+ encodeURI(strJson),
success: function (response) {
Ext.Msg.alert("提交成功", response.responseText); //必须返回json类型 context.Response.Write("{ success: true, errors:{} }");
},
failure: function (response) {
Ext.Msg.alert("提交失败", response.responseText); //必须返回json类型{ success: false, errors:{info: '错误了'} }
}
});
}
}
}]
})

get

2、后台解析json

 String strJson =     new String(request.getParameter("json").getBytes("iso-8859-1"),"utf-8");
java.net.URLDecoder.decode(strJson, "UTF-8");
System.out.println(strJson);
JSONArray js=JSONArray.fromObject(strJson);
JSONObject json = null;
Iterator it=js.iterator();
while(it.hasNext()){
json=(JSONObject)it.next();
String nbbm=json.getString("nbbm");
int flag = json.getInt("flag");
}

get/servlet

下面是post方式传参,推荐这种方法,无须转码:

         bbar:new Ext.PagingToolbar({
emptyMsg:"没有数据",
displayInfo:true,
displayMsg:"目前显示第 {0} - {1} 条,共 {2} 条",
store:store,
pageSize:20,
refreshText:"刷新列表",
items:['-', {
text:'保存',
handler:function(){
var m = store.modified.slice(-1);
var jsonArray = []; //定义修改后的JSON对象
Ext.each(m, function (item) {//将修改后的行对象生成json对象
jsonArray.push(item.data);
});
var strJson = Ext.encode(jsonArray);
if (jsonArray.length == 0) {
Ext.Msg.alert('提示', '没有对数据进行任何更改!');
return;
}else{
Ext.Msg.alert('提示', '保存!');
Ext.Ajax.request({
url: '/servlet/AccountManagement?action=save',
success: function (response) {
Ext.Msg.alert("提交成功", response.responseText); //必须返回json类型 context.Response.Write("{ success: true, errors:{} }");
},
failure: function (response) {
Ext.Msg.alert("提交失败", response.responseText); //必须返回json类型{ success: false, errors:{info: '错误了'} }
},
params: { UpdateInfo: Ext.encode(jsonArray)}//参数使用 Ext.encode方法将JSON对象编码成字符串,传递到后台!!!!!!
});
}
}
}]
})

post

后台解析json:

 String strJson = request.getParameter("UpdateInfo");
System.out.println(strJson);
JSONArray js=JSONArray.fromObject(strJson);
JSONObject json = null;
Iterator it=js.iterator();
while(it.hasNext()){
json=(JSONObject)it.next();
String nbbm=json.getString("nbbm");
int flag = json.getInt("flag");
}

post/servlet