如何设置默认字段值以从当前行在jqgrid中添加表单 [英] How to set default field values for add form in jqgrid from current row

查看:102
本文介绍了如何设置默认字段值以从当前行在jqgrid中添加表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加工具栏按钮用于向jqgrid添加新行. 添加显示的表单,其中包含所有归档的空文件. 如何在发出添加命令时从当前/选定的行的列值中设置添加表单字段值? 使用json远程数据.或者,如果这更简单,那么如何调用传递当前/选定行的服务器方法来从服务器中检索用于添加表单的默认值?

Add toolbar button is used to add new row to jqgrid. Add form which appears contains all filed vlaues empty. How to set add form field values from column values from row which was current/selected when add command was issued ? json remote data is used. Or if this is simpler, how to call server method passing current/selected row to retrieve default values for add form from server ?

jqgrid也包含隐藏的列.如果保存了添加表单,则还应将当前行隐藏列中的可能值也发送到添加控制器.

jqgrid contains also hidden columns. If possible values from hidden columns from current row should also sent to add controller if add form is saved.

更新

我尝试通过使用Oleg伟大的建议

I tried to use Oleg great suggestion by using

         afterShowForm: function(formID) {
           var selRowData, 
               rowid = grid.jqGrid('getGridParam', 'selrow');
           if (rowid === null) {
             // todo: how to cancel add command here
             alert('Please select row');
             return;
             }

           selRowData = grid.jqGrid('getRowData', rowid);
           if (selRowData === null) {
             alert('something unexpected happened');
             return;
             }

           $('#' + 'Baas' + '.FormElement', formID).val(selRowData.Baas);
           }

应用程序在保存后使添加表单保持打开状态.第一次保存后,Baas字段为空.看起来afterShowForm事件仅运行一次,而不是每次保存后都运行.如何解决此问题,以便可以在不关闭添加表单的情况下添加具有默认值的多行? 如果没有选定的行,如何取消或不允许添加命令?

Application keeps add form open after saving. After first save Baas field is empty. It looks like afterShowForm event runs only once, not after every save. How to fix this so that multiple rows with default values can added without closing add form? How to cancel or not allow Add command if there is no selected row ?

推荐答案

如果仅需要对添加"表单进行一些初始化操作,则可以使用至少两种方法:

If you need to make some initialization actions only for Add form you can use at least two approaches:

  • defaultValue属性作为 editoptions内部函数的用法.回调函数defaultValue可以基于所选行中的数据为添加"表单的相应字段提供值.出于优化目的,您可以在 beforeInitData 回调/事件.您可以只读取所需的数据,也可以对服务器进行同步调用以获取所需的信息.使用defaultValue属性的唯一缺点是它使用 jQuery.val 方法来设置添加表单的所有字段的默认值,例外为'checkbox'edittype.对于false0nooffundefined复选框的jqGrid set复选框,在defaultValue属性返回的值中找不到.因此,该方法不适用于其他编辑类型.例如,对于 custom 编辑类型,它可能会失败. /li>
  • beforeShowForm afterShowForm .在回调函数内部,您可以设置表单的任何值.您可以通过与相应列的name属性值相同的字段ID找到网格中相应列的字段.
  • the usage of defaultValue property as function inside of editoptions. The callback function defaultValue can provide the value for the corresponding field of the Add form based of the data from selected row. For optimization purpose you can read the data from the current selected row once in the beforeInitData callback/event. You can just read the data which you need or make an synchronous call to the server to get the information which you need. The only disadvantage of the usage of defaultValue property is that it uses jQuery.val method to set the default value for all fields of Add form with exception 'checkbox' edittype. For the checkboxs jqGrid set checked property of the checkbox of false, 0, no, off or undefined are not found in the value returned by defaultValue property. So the approach will not work for other edittypes. For example it can fail for the custom edittype.
  • the usage of beforeShowForm or afterShowForm. Inside of the callback functions you can set any value of the form. You can find the filed of the corresponding column of the grid by id of the field which is the same as the value of name property of the corresponding column.

就像您已经知道的那样,您可以获取关于getGridParam的当前所选行的ID,并从所选行获取数据

Like you already knows you can get the id of the current selected row with respect of getGridParam and get the data from the selected row

var selRowData, rowid = grid.jqGrid('getGridParam', 'selrow');
if (rowid !== null) {
    // a row of grid is selected and we can get the data from the row
    // which includes the data from all visible and hidden columns
    selRowData= grid.jqGrid('getGridParam', 'selrow');
}

其中,grid是类似于$('#list')的jQuery对象,它选择网格的主要<table>元素.

where grid is jQuery object like $('#list') which selects the main <table> element of the grid.

演示中,您可以看到第一种方法的工作原理以上.

In the demo you can see how works the first approach described above.

这篇关于如何设置默认字段值以从当前行在jqgrid中添加表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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