Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default field values for add form in jqgrid from current row

Tags:

jqgrid

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 contains also hidden columns. If possible values from hidden columns from current row should also sent to add controller if add form is saved.

Update

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

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 ?

like image 792
Andrus Avatar asked Oct 09 '22 15:10

Andrus


1 Answers

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

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

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.

like image 146
Oleg Avatar answered Oct 13 '22 10:10

Oleg