【问题标题】:jqGrid not exiting edit mode after saving (RESTful API)jqGrid 保存后不退出编辑模式(RESTful API)
【发布时间】:2013-12-13 10:01:55
【问题描述】:

我在一个包含两个 jqGrid 的页面的前端工作。其中一个网格完美运行,双击一行时它会启动编辑模式,并在按下Return 键以提交更改后退出编辑模式。另一个(给我带来麻烦的那个)正在工作并在按下Return 时发出正确的 POST 请求,但它仍处于编辑模式。

奇怪的是,两个网格使用相同的代码,只是更改了POST 参数和POSTPUTDELETE URL。我相信问题出现在我用来将POST 数据转换为 JSON 格式的函数中:如果未设置此函数中的所有必需参数,因此服务器在尝试保存时返回错误,则网格退出编辑模式。但是当所有参数设置正确并且POST请求成功时,网格保持不变。工作正常的网格使用非常相似的函数将 POST 数据转换为 JSON 格式。

网格的(相关)代码:

function loadCommissions(userid, accountid) {
    $("#pager-commissions").empty();
    $("#jqgrid-commissions").empty().jqGrid({
        url : '/api/v1/commission/?account=' + accountid,
        datatype : 'json',
        ajaxGridOptions : {
            contentType : "application/json"
        },
        jsonReader : {
            repeatitems : false,
            id : "id"
        },
        colNames : ["Commission", "Type", "Rate"],
        colModel : [{
            name : 'Commission',
            width : 202
        }, {
            name : 'Type',
            edittype : 'select',
            formatter : 'select',
            editoptions : {
                value : 'F:Fixed Amount;P:Percentage'
            },
            stype : 'select'
        }, {
            name : 'Rate'
        }],
        cmTemplate : {
            editable : true,
            width : 100
        },
        ondblClickRow : function() {
            var rowid = $("#jqgrid-commissions").getGridParam('selrow');
            $('#jqgrid-commissions').jqGrid('editRow', rowid, {
                keys : true,
                dataType : 'json',
                url : '/api/v1/commission/' + encodeURIComponent(rowid) + '/',
                mtype : 'PUT'
            });
        },
        serializeRowData : function(data) {
            return serializeCommissionData(data, userid);
        },
        ajaxRowOptions : {
            contentType : "application/json"
        },
        autowidth : true,
        shrinkToFit : false,
        gridview : true,
        height : "auto",
        autoencode : true,
        loadonce : false,
        rowNum : 10,
        rowList : [10, 20, 30],
        pager : "#pager-commissions",
        viewrecords : true
    });
}

POST数据转换为JSON格式的函数(服务器预期):

function serializeCommissionData(data, userid) {
    return JSON.stringify({
        user : '/api/v1/user/' + userid + '/',
        account : '/api/v1/account/' + $("a.jstree-clicked:first").parent().attr("id") + '/',
        Type : data.Type,
        Commission : data.Commission,
        Rate : data.Rate
    });
}

检索到网格使用的 JSON (/api/v1/commission/?account=1):

{
  "rows": [
    {
      "commission_currency": null, 
      "account": "/api/v1/account/1/", 
      "Commission": "Test", 
      "Rate": "0", 
      "user": "/api/v1/user/1/", 
      "Type": "F", 
      "id": 5, 
      "resource_uri": "/api/v1/commission/5/"
    }
  ], 
  "meta": {
    "total_count": 1, 
    "next": null, 
    "records": 20, 
    "limit": 20, 
    "offset": 0, 
    "total": 1, 
    "page": 1, 
    "previous": null
  }
}

【问题讨论】:

    标签: jquery ajax json jqgrid jstree


    【解决方案1】:

    我发现了问题。在网格的PUT 请求中,服务器返回HttpNoContent (204),而不是HttpResponse (200)。我将后端更改为返回 200 而不是 204,这样就成功了。

    【讨论】:

      猜你喜欢
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      相关资源
      最近更新 更多