jqGrid中是否存在一個事件來在添加新行後執行操作?如何在添加新行後在jqGrid中執行操作
我在jqGrid wiki中看到afterInsertRow中有事件,但顯然只要jqGrid在顯示錶格時「插入」行到表中就觸發它。
那麼當用戶「插入」(保存)新行後我想要做些什麼時,應該使用什麼?或者,是否有任何可以讓我「知道」新行被添加的變量?
jqGrid中是否存在一個事件來在添加新行後執行操作?如何在添加新行後在jqGrid中執行操作
我在jqGrid wiki中看到afterInsertRow中有事件,但顯然只要jqGrid在顯示錶格時「插入」行到表中就觸發它。
那麼當用戶「插入」(保存)新行後我想要做些什麼時,應該使用什麼?或者,是否有任何可以讓我「知道」新行被添加的變量?
主要的問題是,要能夠選擇你需要知道新行的id的行。在大多數情況下,id將由您將數據保存在服務器上的數據庫生成。因此,對服務器代碼的第一個要求是在「add」操作的服務器響應中的新行上返回id。
例如,您的服務器代碼返回您的行ID作爲「添加」操作的響應。
$("#list").jqGrid('navGrid', '#pager', {/*navGrid options*/}, {/*Edit optoins*/}, {
/*Add options:*/
reloadAfterSubmit: false,
afterSubmit: function (response) {
return [true, '', response.responseText];
},
addedrow: "last", // add new row at the end of grid
afterComplete: function (response, postdata) {
// this.gbox is the string like "#gbox_list"
var gridId = this.gbox.substr(6);
//if (postdata.oper === "add") {
// // highlight the new "added" row
// var row = $("#" + $.jgrid.jqID(postdata.id));
// row.effect("highlight", {}, 3000);
//}
$('#' + gridId).jqGrid('setSelection', postdata.id);
}
});
在afterComplete
我展示瞭如何使用jQuery UI highlight效果突出新添加的行(見the old answer)的評論部分。它可以替代選擇行。您也可以使用選擇和突出顯示效果。
選項reloadAfterSubmit: false
至少有兩個缺點。
sortname
參數不爲空)網格的行會不正確排序後的新行會被添加作爲第一個或最後一個行在網格中。rowNum
參數定義),則新行的添加將隨每個頁面行數過多而增加。所以,你可以做以下
var idToSelect;
$("#list").jqGrid({
// ... all jqGrid options
loadComplete: function() {
if (idToSelect) {
$(this).jqGrid('setSelection', idToSelect);
//$("#" + $.jgrid.jqID(idToSelect)).effect("highlight", {}, 3000);
idToSelect = undefined;
}
}
}).jqGrid('navGrid', '#pager', {/*navGrid options*/}, {/*Edit optoins*/}, {
/*Add options:*/
afterSubmit: function (response) {
// save the id of new row. If the format of the data returned from
// the server is different you should change the next row
// corresponds to the returned data. For example if the server returns
// back JSON data in the form {"myId":"123"} you should use
// $.parseJSON(response.responseText).myId
// instead of response.responseText below
idToSelect = response.responseText;
return [true, '', response.responseText];
}
});
在新添加的行會電網的重載後選擇的情況。
您應該更確切地描述您使用哪種「插入」。例如,你使用表單編輯。用戶點擊「添加」按鈕,用戶點擊「提交」按鈕後,數據將成功保存在服務器上。在收到數據成功添加的服務器響應後,您想要執行一些操作(修改服務器響應)。你從服務器發回新行的'id'還是不? – Oleg
是的,用戶點擊「添加」按鈕,填寫新行數據,點擊「提交」按鈕,創建表中的新行。我不需要修改服務器響應。在這種特殊情況下,我只需要使用setSelection方法選擇表中的第一行,但是如果添加了新行,則需要防止該行爲。這就是爲什麼我要爲此尋找事件。 – cincplug
而且我有從服務器發送的行ID。 – cincplug