jqGrid(删除行) - 如何发送额外的 POST 数据? [英] jqGrid (Delete row) - How to send additional POST data?

查看:17
本文介绍了jqGrid(删除行) - 如何发送额外的 POST 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 jqGrid 删除机制的问题,因为它只以 POST 数据的形式发送oper"和id"参数(id 是表的主键).

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).

问题是,我需要根据 id 和另一个列值删除一行,比如说 user_id.如何将此 user_id 添加到 POST 数据中?

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?

我可以将问题总结如下:

I can summarize the issue as the following:

  1. 如何获取选中行的单元格值(user_id)?
  2. 以及,如何将该 user_id 添加到 POST 数据中,以便可以从发生实际删除过程的代码中检索它.

示例代码:

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
        );

推荐答案

没问题.有不同的方法来做你需要的.最直接的方法是使用 serializeDelData 函数.jqGrid删除行的代码如下所示

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

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);

所以你可以定义你自己的 serializeDelData 函数来满足你的所有需要​​:

So you can just define your own serializeDelData function which do all what you need:

{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

顺便说一句,如果您想自己生成发布到服务器的数据,只需返回一个字符串而不是一个对象.然后数据将由 jQuery.ajax 发布而不做任何更改.

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.

如果您不想在发布的数据中放置附加信息,但在 URL 中,您可以在 onclickSubmit 中执行此操作.例如,我在服务器端使用 RESTfull 服务并删除一个项目,我使用 DELETE HTTP 请求和空主体.所有参数都放在 URL 中.对应的代码如下:

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:

{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

这篇关于jqGrid(删除行) - 如何发送额外的 POST 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆