ここでは、RESTfulサービスを使用する場合に何をすべきかについて詳しく説明しました。コールバックonclickSubmit
は、URLを動的に変更し、id
それに追加するのに最適な場所です。
githubにあるjqGridの現在のコードは、グリッドのDOM要素を。として設定しますthis
。したがって、次のものを使用できるようになります
onclickSubmit: function (options, postdata) {
options.url += '/' + encodeURIComponent(postdata.[this.id + "_id"]);
}
次の(4.3.1以降の)バージョンのjqGridで。jqGridの現在のバージョンでは、コードは次のようになります
var $grid = $("#eventGrid");
// set defaults for Delete form
$.extend($.jgrid.del, {
mtype: "DELETE",
reloadAfterSubmit: false
serializeDelData: function () {
return ""; // don't send and body for the HTTP DELETE
},
onclickSubmit: function (options, postdata) {
options.url += '/' + encodeURIComponent(postdata);
}
});
// set defaults for Edit and Add forms
$.extend($.jgrid.edit, {
height: 280,
reloadAfterSubmit: false,
onclickSubmit: function (options, postdata) {
// options.gbox will be like "#gbox_eventGrid"
var gridId = options.gbox.substr(6), // cut id from the gbox selector
id = postdata.[gridId + "_id"];
if (id !== "_empty") {
options.url += '/' + encodeURIComponent(id);
}
},
afterSubmit: function (responseData) {
// in case of usage reloadAfterSubmit: false it's important
// that the server returns id of the new added row
// in the simplest form the server response should just contain
// the id. In more complex response one should modify the next line
// to extract the id only from the response
return [true, '', responseData];
}
});
// create jqGrid
$grid.jqGrid({
// all other parameters
editurl: "/eventInfo" // you should use relative paths
});
// create navigator layer
$grid.jqGrid('navGrid', '#pager', {/*navGrid options*/}, { mtype: 'PUT'});
上記のコードでは、オプションafterSubmit
を使用するために重要なものを追加しましたreloadAfterSubmit: false
。
備考:私は主に上記のコードを入力するか、カットアンドペーストを使用して別の古い回答からいくつかの部分をコピーしました。したがって、アプリケーションで上記のコードをテストする必要があります。