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 w
If you need to make some initialization actions only for Add form you can use at least two approaches:
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.name
property of the corresponding column.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');
}
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.