问题描述
添加工具栏按钮用于向 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 ?
推荐答案
如果你只需要对 Add form 做一些初始化操作,你至少可以使用两种方法:
If you need to make some initialization actions only for Add form you can use at least two approaches:
defaultValue
属性在 编辑选项.回调函数defaultValue
可以根据选中行的数据为Add表单的对应字段提供值.出于优化目的,您可以在 beforeInitData 回调/事件.您可以只读取您需要的数据或对服务器进行同步调用以获取您需要的信息.使用defaultValue
属性的唯一缺点是它使用 jQuery.val 方法为添加表单的所有字段设置默认值,异常'checkbox'edittype
.对于复选框 jqGrid 设置复选框的选中属性false
、0
、no
、off
或在
.因此该方法不适用于其他编辑类型.例如,对于 custom 编辑类型,它可能会失败.defaultValue
属性返回的值中找不到 undefined- beforeShowForm 或 .在回调函数内部,您可以设置表单的任何值.可以通过与对应列的
name
属性值相同的字段的id,找到网格对应列的字段.
- the usage of
defaultValue
property as function inside of editoptions. The callback functiondefaultValue
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 ofdefaultValue
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 offalse
,0
,no
,off
orundefined
are not found in the value returned bydefaultValue
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 中添加表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!