如何在内联添加jqGrid后更新列

时间:2012-03-04 16:55:10

标签: javascript jqgrid

jqGrid包含在colmodel中定义为

的列
{"name":"_actions",
  "formatoptions":{"editbutton":true,"keys":true
  ,"delbutton":true
    } },

{ "name":"Kood","editable":true,"hidden":true}

在工具栏中按下内联添加按钮的网格中添加了新行。 保存数据后,控制器返回新的Kood列值。 此新值应分配给Kood列。

下面的代码显示了我尝试的两种方法,但都失败了。 Kood列值不会改变

如何在内联添加后更新列? 如果使用保存操作按钮保存内联添加的行,如何更新列?

$grid.jqGrid('inlineNav', '#grid_toppager', {
  addParams: {
     addRowParams: {
            keys: true,
            // This is called if enter key is pressed to save added row
            aftersavefunc: afterSaveFuncAfterAdd,
        }
    },

  editParams: {
      keys: true,
      // This is called if saver button in toolbar is pressed on inline add
      aftersavefunc: afterSaveFuncAfterAdd,
    },
add: true,
edit: false,
save: true,
cancel: true
});

function afterSaveFuncAfterAdd(rowID, response ) {
  var json = $.parseJSON(response.responseText);
      postData = $grid.jqGrid('getGridParam', 'postData');
  // this shows correct value:
  alert(json.PrimaryKeyValues[0]);
  // attempt 1:
  $('#' + rowID + '_Kood').val(json.PrimaryKeyValues[0]);
  // attempt2:
  postData['Kood'] = json.PrimaryKeyValues[0];
  }

1 个答案:

答案 0 :(得分:5)

编辑后将调用editRow的回调aftersavefunc。目前你找不到$('#' + rowID + '_Kood')。此外,postData在内联编辑期间不会更改,因此$grid.jqGrid('getGridParam', 'postData')将不会为您提供有用的信息。

因此,我建议您回复editurl作为回复所需的所有数据。例如,具有服务器计算的默认值的列,如上次编辑时间戳,您可以回发。 Add操作的响应还应包含服务器生成的id。在收到服务器的响应后,您可以使用setRowDatasetCell来修改网格中的数据。

例如,您可以返回

{"Id": "DB_Id", "Kood": "new Kood value"}

从服务器作为“添加”操作的响应并返回

{"Kood": "new Kood value"}

作为“编辑”操作的响应。如果afterSaveFuncAfterAdd的代码可能类似于以下

var afterSaveFunc = function (rowId, response) {
    var data = $.parseJSON(response.responseText);
    if ($.isPlainObject(data) && typeof data.Kood !== "undefined") {
        if (typeof data.Id !== "undefined") {
            // if we have 'Id' column in the grid we have to update the column
            // together with the value of `Kood`
            $(this).jqGrid('setRowData', rowId, { Id: data.Id, Kood: data.Kood });
            // if we have no additional `Id` we can update the Kood column only with
            // the statement like below
            // $(this).jqGrid('setCell', rowId, 'Kood', data.Kood);

            // in case of "Add" operation we have to update the row Id
            $('#' + $.jgrid.jqID(rowId)).attr('id', data.Id);
        } else {
            $(this).jqGrid('setCell', rowId, 'Kood', data.Kood);
        }
    }
}