嗨,我是jqGrid的新手。我正在使用jqGrid 4.4。我已经成功地使用了Oleg的方法
Adding new row to jqGrid using modal form on client only
在本地添加行。我面临着两个问题
我总是在错误块中收到警报?
$("#sendButton").click(function(){
var gridData = jQuery("#list").getRowData();
var myJSONString = JSON.stringify(gridData);
var postData = myJSONString.replace(/[\"]/g, '\"');
alert("JSON serialized jqGrid data:\n" + postData);
$.ajax({
type: "POST",
url: CONTEXT_ROOT+"/json",
data : postData,
dataType:"json",
contentType: "application/json; charset=utf-8",
success: function(response, textStatus, xhr) {
alert("success");
},
error: function(xhr, textStatus, errorThrown) {
alert("error:"+errorThrown+" textStatus : "+textStatus);
}
});
});
一旦我保存在java控制器中,不确定我需要返回什么。这是控制器中的代码。我也试过返回为无效但结果相同。还有一种更好的方法将来自jsp的json对象列表绑定到域对象列表。我已经尝试了Binding,只有一个对象,比如使用@RequestBody的表单,如
中所述http://blog.springsource.org/2010/01/25/ajax-simplifications-in-spring-3-0/
@RequestMapping(value = "/json", method = RequestMethod.POST)
public ModelAndView saveNewCasePackOptions(@RequestBody List<Map> json) {
for(Map mJson : json){
String idCasePkOptions = (String)mJson.get("idCasePackOptions");
Long idCasePackOptions = (idCasePkOptions.isEmpty())?null:new Long(idCasePkOptions);
Short cypharecommended = new Short((String)mJson.get("cypharecommended"));
Short distributorapproved = new Short((String)mJson.get("distributorapproved"));
String heightStr = (String)mJson.get("height");
Double height = (heightStr.isEmpty())?null:new Double(heightStr);
String lengthStr = (String)mJson.get("length");
Double length = (lengthStr.isEmpty())?null:new Double(lengthStr);
String weightStr = (String)mJson.get("height");
Double weight = (weightStr.isEmpty())?null:new Double(weightStr);
String widthStr = (String)mJson.get("width");
Double width = (widthStr.isEmpty())?null:new Double(widthStr);
String stateString = (String)mJson.get("statuscode");
stateString = (stateString.contains("Green"))?"1":"0";
Short statuscode = new Short(stateString);
CasePackOptions casePkOpt = new CasePackOptions(idCasePackOptions, cypharecommended, distributorapproved, height, length, statuscode, weight, width);
System.out.println(casePkOpt);
casePackOptionsService.save(casePkOpt);
}
ModelAndView mav = new ModelAndView();
mav.setViewName("casepackoptions/listPage1.jsp");
return mav;
}
非常感谢任何帮助。
答案 0 :(得分:1)
我在问题的第一部分here上写了答案。
你可以从使用Spring的人那里得到问题第二部分的答案。部分myJSONString.replace(/[\"]/g, '\"')
似乎对我很怀疑。您可能需要使用data : {json: postData}
代替data : postData
。您可能需要将@RequestBody
更改为@RequestParam
或@RequestParam("json")
。我自己不使用Spring。
此外,您可以使用$("#list").jqGrid("getGridParam", "data")
代替jQuery("#list").getRowData()
。如果您使用本地数据分页,它将特别有用。
答案 1 :(得分:0)
我已经开始工作,但不确定这是否是推荐方式。
java脚本函数是
$("#sendButton").click(function(){
var gridData = jQuery("#list").getRowData();
gridData = JSON.stringify(gridData);
alert("postData stringify data :\n" + postData);
postData = postData.replace();
$.ajax({
type: "POST",
url: CONTEXT_ROOT+"/json",
data : gridData,
contentType: "application/json; charset=utf-8",
success: function(response, textStatus, xhr) {
alert("success");
},
error: function(xhr, textStatus, errorThrown) {
alert("error:"+errorThrown+" textStatus : "+textStatus);
}
});
});
服务器代码现在是
@RequestMapping(value = "/json", method = RequestMethod.POST)
public @ResponseBody String saveNewCasePackOptions(@RequestBody List<Map> json) {
for(Map mJson : json){
String idCasePkOptions = (String)mJson.get("idCasePackOptions");
Long idCasePackOptions = (idCasePkOptions.isEmpty())?null:new Long(idCasePkOptions);
Short cypharecommended = new Short((String)mJson.get("cypharecommended"));
Short distributorapproved = new Short((String)mJson.get("distributorapproved"));
String heightStr = (String)mJson.get("height");
Double height = (heightStr.isEmpty())?null:new Double(heightStr);
String lengthStr = (String)mJson.get("length");
Double length = (lengthStr.isEmpty())?null:new Double(lengthStr);
String weightStr = (String)mJson.get("height");
Double weight = (weightStr.isEmpty())?null:new Double(weightStr);
String widthStr = (String)mJson.get("width");
Double width = (widthStr.isEmpty())?null:new Double(widthStr);
String stateString = (String)mJson.get("statuscode");
Short statuscode = new Short(stateString);
CasePackOptions casePkOpt = new CasePackOptions(idCasePackOptions, cypharecommended, distributorapproved, height, length, statuscode, weight, width);
System.out.println(casePkOpt);
casePackOptionsService.save(casePkOpt);
}
return "Success";
}
感觉需要以不同方式完成的事情
JSON =%5B%7B%22idCasePackOptions%22%3A%221%22%2C%22cypharecommended%22%3A%221%22%2C%22distributorapproved%22%3A%222%22%2C%22height%22% 3A%2214%22%2C%22length%22%3A%2255%22%2C%22statuscode%22%3A%221%22%2C%22weight%22%3A%2214%22%2C%22width%22%3A% 221%22%7D%2C
任何建议都会有所帮助。感谢