如何在 jqgrid 中使用 editData 或 onclickSubmit 发布可变数据 [英] How to post variable data with editData or onclickSubmit in jqgrid

查看:17
本文介绍了如何在 jqgrid 中使用 editData 或 onclickSubmit 发布可变数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 editData 或 onclickSubmit 来执行我需要的操作.

I can't get editData or onclickSubmit to do what I need.

我希望网格在更新后跟随添加或编辑的行.所以,我需要发布一些额外的信息,以便服务器可以返回添加/编辑记录的 id 和正确页面.

I want the grid to follow the added or edited row after update. So, I need to post some additional info so the server can return the id and correct page of the added/edited record.

我可以使用 addfunc 和 editfunc 以及自定义表单来做到这一点,但我想使用 jqgrid 生成的表单来做到这一点.

I was able to do this using the addfunc and editfunc and a custom form, but I'd like to do it with the jqgrid generated forms.

我在 DocumentReady 函数之前声明了一个全局变量.然后,我尝试在编辑参数中使用 editData 并在 beforeSubmit 或 beforeInitData 中设置变量.变量发布到服务器,但仅在它们最初声明时发布.似乎 editData 是在初始化时创建的,无法更新.我也尝试使用 onclickSubmit,但也无法正常工作.

I have a global declared before the DocumentReady function. Then, I tried using editData in the edit parameters and setting the variables in beforeSubmit or beforeInitData. The variables are posted to the server, but only as they are initially declared. It seems like the editData is created at initialization and can't be updated. I also tried using onclickSubmit, but couldn't get that to work either.

这是一个经过编辑的示例:

Here's an edited example:

var data2pass = {};
    data2pass['sortColumnName'] = '';
    data2pass['sortOrder'] = '';
    data2pass['rowNum'] = '';

$(document).ready(function(){

  $("#ProdGrid").jqGrid({
    url:'products_DAT.php?thespot=server_ProdGrid',
    datatype: 'json',
    mtype: 'POST',
    colNames:['ID','Product Name:','Category:','Unit Price:'],
    colModel :[ 
      {name:'ProductID', editable:true},
      {name:'ProductName', editable:true},
      {name:'CategoryID', editable:true, edittype:"select", editoptions: { dataUrl:  "products_DAT.php?thespot=select4_CategoryID" }},
      {name:'UnitPrice', align:'right', editable:true, formatter:'currency'}
    ],
    pager: '#ProdGrid_pager',
    rowNum: 15,
    sortname: 'ProductName',
    sortorder: 'asc',
    gridview: true,
    editurl: 'products_DAT.php?thespot=update_ProdGrid',
    height: 'auto'
  });

$("#ProdGrid").jqGrid('navGrid','#ProdGrid_pager', {},
{closeAfterEdit:true, reloadAfterSubmit: false, editData: data2pass,
    beforeInitData: function(formid) { 
    data2pass['sortColumnName'] = 'ProductName';
    data2pass['sortOrder'] = 'asc';
    data2pass['rowNum'] = '15';
    }
}, // Edit parameters
{}, // Add Parameters
{reloadAfterSubmit:true}, // Delete parameters
{}, // Search params
{} // View params
 );

然而,最初声明的 data2pass 变量是发布到服务器的内容.我应该使用什么事件来更新 data2pass 的值以发布到服务器?或者还有其他更好的方法吗?

However the data2pass variables are initially declared is what gets posted to the server. What event should I use to update the values of data2pass to post to the server? Or is there another better way to do it?

非常感谢任何建议.

谢谢

推荐答案

您可以使用函数定义 editData 的属性

You can either define properties of editData using functions

editData: {
    sortColumnName: function () { return "ProductName"; },
    sortOrder: function () { return "asc"; },
    rowNum: function () { return 15; }
}

或者使用回调onclickSubmit来扩展发布到服务器的数据

or use callback onclickSubmit to extend the data posted to the server

onclickSubmit: function (options, postData) {
    return {
        sortColumnName: "ProductName",
        sortOrder: "asc",
        rowNum: 15
    };
}

或使用 serializeEditData 回调

serializeEditData: function (postData) {
    return $.extend(true, {}, postData, {
        sortColumnName: "ProductName",
        sortOrder: "asc",
        rowNum: 15
    });
}

上面的每一种方法都是一样的.您可以选择一种最适合您要求的方式.

Every from above ways do the same. You can choose one way which you find the mostly convenient for your requirements.

这篇关于如何在 jqgrid 中使用 editData 或 onclickSubmit 发布可变数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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