[英]How to make jquery row non editable and save the same row in Database using ajax call?
I made the inline edit true for jqGrid by using below code - 我使用以下代码对jqGrid进行了内联编辑:
if (rowid) {
if (rowid !== lastsel) {
$("#prjectForecastData").jqGrid('restoreRow', lastsel);
$("#prjectForecastData").jqGrid('editRow', rowid, true);
lastsel = rowid;
} else {
$("#prjectForecastData").jqGrid('restoreRow', lastsel);
lastsel = "";
}
}
But here in below code, while saving row, I want to make an ajax call in order to save that row in database. 但是在下面的代码中,在保存行的同时,我想进行ajax调用以将该行保存在数据库中。 and also want to make that row non editable. 并且还希望使该行不可编辑。 Right now 'clientArray'
is used to save the value on browser temporarily.But unable to make it non editable after that. 现在, 'clientArray'
用于临时保存浏览器中的值,但是之后无法使其不可编辑。
$("#saveForecastEditData").click(function(){
$("#prjectForecastData").jqGrid('saveRow',lastsel, false, 'clientArray');
jQuery('#prjectForecastData').editRow(lastsel,false);
});
So how to make an ajax call and make the row non editable after saving the row in DB successfully? 那么,如何在将行成功保存到DB中之后进行ajax调用并使该行不可编辑?
To make row non-editable you should add not-editable-row
class to the corresponding row. 要使行不可编辑,应将not-editable-row
类添加到相应的行。 The exact code can depend on jqGrid configuration which you use. 确切的代码可以取决于您使用的jqGrid配置。 For example you can just add the line 例如,您可以添加行
$("#" + lastsel).addClass("not-editable-row");
after the line with saveRow
, but sorting or paging of local data will recreate the grid body and so it will remove any previously set classes or rows of the grid. 在带有saveRow
的行saveRow
,但是对本地数据进行排序或分页将重新创建网格主体,因此它将删除任何先前设置的网格类或行。 To make the information persistent you can save it in local data
together with the main data. 为了使信息的持久,您可以在本地保存data
连同主数据。 For example you can do the following 例如,您可以执行以下操作
$("#saveForecastEditData").click(function () {
var $myGrid = $("#prjectForecastData"), rowData;
$myGrid.jqGrid("saveRow", lastsel, { url: "clientArray"});
rowData = $myGrid.jqGrid("getLocalRow", lastsel);
// add new property to the data
rowData.modified = true;
// add the "not-editable-row" immediately
$("#" + lastsel).addClass("not-editable-row");
});
The code of "#prjectForecastData"
grid can use rowattr
: "#prjectForecastData"
网格的代码可以使用rowattr
:
$("#prjectForecastData").jqGrid({
// ... all your existing parameters here
rowattr: function (rowData) {
if (rowData.modified) {
return {"class": "not-editable-row"};
}
}
});
It will restore the class "not-editable-row"
on rows which was modified locally. 它将在本地"not-editable-row"
上还原类"not-editable-row"
。
To get all modified rows and to send there to the server one can use the following code 要获取所有已修改的行并将其发送到服务器,可以使用以下代码
var data = $("#prjectForecastData").jqGrid("getGridParam", "data");
var modifiedData = $.grep(data, function (item) { return item.modified; } );
$.ajax({
url: "someServerUtl",
type: "POST",
dataType: "json",
data: {
modifications: JSON.stringify(modifiedData)
}
});
The exact parameters of $.ajax
request will depend on your server code which get the data. $.ajax
请求的确切参数将取决于获取数据的服务器代码。
In the way you will save all modified data using one ajax call. 这样,您将使用一个ajax调用保存所有修改的数据 。 Alternativaly you can just use another (server based) url
in the call of saveRow
. 另外,您可以在saveRow
调用中使用另一个(基于服务器的) url
。 In the case the data of one saved row will be automatically send to the server on in the format described in the documentation . 如果保存的一行数据将自动以文档中所述的格式发送到服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.