jQuery jqGrid(删除行) - 如何发送额外的 POST 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2833254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jqGrid (Delete row) - How to send additional POST data?
提问by ronanray
I'm having problem with jqGrid delete mechanism as it only send "oper" and "id" parameters in form of POST data (id is the primary key of the table).
我遇到了 jqGrid 删除机制的问题,因为它只以 POST 数据的形式发送“oper”和“id”参数(id 是表的主键)。
The problem is, I need to delete a row based on the id and another column value, let's say user_id. How to add this user_id to the POST data?
问题是,我需要根据 id 和另一个列值删除一行,比如说 user_id。如何将此 user_id 添加到 POST 数据中?
I can summarize the issue as the following:
我可以将问题总结如下:
- How to get the cell value (user_id) of the selected row?
- AND, how to add that user_id to the POST data so it can be retrieved from the code behind where the actual delete process takes place.
- 如何获取所选行的单元格值(user_id)?
- 并且,如何将该 user_id 添加到 POST 数据中,以便可以从发生实际删除过程的代码中检索它。
Sample codes:
示例代码:
jQuery("#tags").jqGrid({
url: "subgrid.process.php,
editurl: "subgrid.process.php?,
datatype: "json",
mtype: "POST",
colNames:['id','user_id','status_type_id'],
colModel:[{name:'id', index:'id', width:100, editable:true},
{name:'user_id', index:'user_id', width:200, editable:true},
{name:'status_type_id', index:'status_type_id', width:200}
],
pager: '#pagernav2',
rowNum:10,
rowList:[10,20,30,40,50,100],
sortname: 'id',
sortorder: "asc",
caption: "Test",
height: 200
});
jQuery("#tags").jqGrid('navGrid','#pagernav2',
{add:true,edit:false,del:true,search:false},
{},
{mtype:"POST",closeAfterAdd:true,reloadAfterSubmit:true}, // add options
{mtype:"POST",reloadAfterSubmit:true}, // del options
{} // search options
);
回答by Oleg
It is not a problem. There are different ways to do what you need. The most direct way is usage of serializeDelData
function. The code of jqGrid which delete rows look like following
这不成问题。有不同的方法可以做你需要的。最直接的方式是使用serializeDelData
函数。删除行的 jqGrid 代码如下所示
var ajaxOptions = $.extend({
url: rp_ge.url ? rp_ge.url : $($t).jqGrid('getGridParam','editurl'),
type: p.mtype,
data: $.isFunction(p.serializeDelData) ? p.serializeDelData(postd) : postd,
complete:function(data,Status){
//...
},
error:function(xhr,st,err){
//...
}
}, $.jgrid.ajaxOptions, p.ajaxDelOptions);
$.ajax(ajaxOptions);
So you can just define your own serializeDelData
function which do all what you need:
所以你可以定义你自己的serializeDelData
函数来完成你需要的所有事情:
{mtype:"POST", reloadAfterSubmit:true, serializeDelData: function (postdata) {
var rowdata = jQuery('#tags').getRowData(postdata.id);
// append postdata with any information
return {id: postdata.id, oper: postdata.oper, user_id: rowdata.user_id};
}}, // del options
By the way if you want produce yourself the data posted to the server, just return a string instead of an object. Then the data will be posted by jQuery.ajax without any changes.
顺便说一句,如果您想自己生成发布到服务器的数据,只需返回一个字符串而不是一个对象。然后数据将被 jQuery.ajax 发布而不做任何更改。
If you prefer to place an additional information not in the data which are posted, but in the URL you can do this inside of onclickSubmit
. I use for example a RESTfull service on the server side and to delete an item I use DELETE HTTP request with the empty body. All parameters are placed in the URL. The corresponding code looks like following:
如果您不想在发布的数据中放置附加信息,而是在 URL 中放置附加信息,您可以在onclickSubmit
. 例如,我在服务器端使用 RESTfull 服务并删除一个项目,我使用带有空正文的 DELETE HTTP 请求。所有参数都放在 URL 中。相应的代码如下所示:
{mtype:"DELETE", reloadAfterSubmit:true, serializeDelData: function (postdata) {
return ""; // the body MUST be empty in DELETE HTTP requests
}, onclickSubmit: function(rp_ge,postdata) {
var rowdata = jQuery('#tags').getRowData(postdata.id);
rp_ge.url = 'subgrid.process.php/' + encodeURIComponent(postdata.id) +
'?' + jQuery.param ({user_id: rowdata.user_id});
}}, // del options
回答by ravl1084
This is how I got to jqGrid to append more data to the POST on delete:
这就是我如何使用 jqGrid 在删除时将更多数据附加到 POST:
var grid = $("#grid").jqGrid(... //make your grid
$("#grid").jqGrid('navGrid', '#pager', {add:false, edit:false, del:true},
{},
{},
{delData: {
name: function() {
var sel_id = grid.jqGrid('getGridParam', 'selrow');
var value = grid.jqGrid('getCell', sel_id, 'colName');
return value;
}
}
},
{},
{});
This will return a post array {id:rowNumber, oper:del, name:someValue}. name
is how you want to refer to your variable in the post array, someValue
is whatever is in the cell from the selected row that interests you.
这将返回一个 post 数组 {id:rowNumber, oper:del, name:someValue}。name
是您希望如何在 post 数组中引用您的变量,someValue
是您感兴趣的所选行的单元格中的任何内容。
Hope this helps!
希望这可以帮助!
回答by Power Engineering
This can be easy achieved if you set key:truein colModellike this:
如果像这样在colModel 中设置key:true,这可以很容易地实现:
colModel: [
{name: 'childId', index: 'childId', align: 'center', sorttype: 'string', key:true},
colModel: [
{name: 'childId', index: 'childId', align: 'center', sorttype: 'string', key:true},
doing that will cause automatically to send this field during POST.
这样做将导致在 POST 期间自动发送此字段。
The intent of key:truein colModelis just to allow the command processor to operate properly on a indexed record set.
colModel中key:true的目的只是为了允许命令处理器在索引记录集上正确运行。
回答by Aditya
Yes you can add additional data to the postdata using onclickSubmit event under del parameters of navgrid, In your case if you want to send user_id including id, than do something like this
是的,您可以在 navgrid 的 del 参数下使用 onclickSubmit 事件将其他数据添加到 postdata,在您的情况下,如果您想发送包含 id 的 user_id,请执行以下操作
$("#gridId").jqGrid({
}).hideCol(['']).navGrid('#pagerId', {
edit: false, add: false, del: true, search: true, refresh: true, beforeRefresh: function () {
}, afterRefresh: function () {
}
}, {}, {}, {
afterComplete: function () {
}, onclickSubmit: function (params) {
var extraParameters = []; var arraySelected = $("#gridId").jqGrid('getGridParam', 'selarrrow');
for (var i = 0; i < arraySelected.length; i++) {
extraParameters.push($('#gridId').getRowData(arraySelected[i]).user_id)
}
return { extraParams: extraParameters.join(',') };
}
},
{
sopt: ['cn', 'nc'], onSearch: function () {
},
onReset: function () {
}
});
回答by bruno
I had the same problem. The way that i fixed it, was to put the user_id as first column in my jsondata. and with the jsonreader settings of the jqgrid you can accomplish it that will work.
我有同样的问题。我修复它的方法是将 user_id 作为我的 jsondata 中的第一列。并且使用 jqgrid 的 jsonreader 设置,您可以完成它的工作。
http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data
http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data
look at the jsonreader settings for jsondata.
查看 jsondata 的 jsonreader 设置。
i use:
我用:
jsonReader: { cell: "", id: "0" }
and my data is like
我的数据就像
{ totalpages: "xxx", currpage: "yyy", totalrecords: "zzz",
invdata : [
{"userid","cell12", "cell13", "cell14"},
{"userid","cell22", "cell23", "cell24"},
...
]
}
and now if i update a row or delete a row i'll get the userid. Hope this can work for you!
现在,如果我更新一行或删除一行,我将获得用户 ID。希望这对你有用!
回答by Wolf
I am not sure how to post additional data, but you can try sending additional data as query string parameters as part of the delete-url.
我不确定如何发布附加数据,但您可以尝试将附加数据作为查询字符串参数作为 delete-url 的一部分发送。