在本地添加行之后将所有数据发送到服务器的问题

时间:2012-09-07 01:14:43

标签: jquery jqgrid

嗨,我是jqGrid的新手。我正在使用jqGrid 4.4。我已经成功地使用了Oleg的方法

Adding new row to jqGrid using modal form on client only

在本地添加行。我面临着两个问题

  1. 未添加到最后的行,我该如何实现?
  2. 我点击按钮发送所有行,如下所示:
  3. 我总是在错误块中收到警报?

        $("#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;
    }
    

    非常感谢任何帮助。

2 个答案:

答案 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";
}

感觉需要以不同方式完成的事情

  1. 当我尝试使用$(“#list”)。jqGrid(“getGridParam”,“data”);而不是jQuery(“#list”)。getRowData();我得到了id:“1”作为我的Json字符串的一部分
  2. 尝试使用数据时:{json:postData}而不是data:postData,firebug中的json对象编码如下
  3. 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

    任何建议都会有所帮助。感谢