5

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 ?

Andrus
  • 26,339
  • 60
  • 204
  • 378

1 Answers1

4

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you. In some cases there is need to set default values for jqueryUI autocomplete fields used for foreign key entry from big lookup tables. autocomplete is implemented as custom editing element. Please confirm, is the best way to use beforeShowForm in this case? Should we use beforeShowForm in other fields also due to this to avoid mixing two methods for setting default value? Where to find sample code which sets default value of jqueryui autocomplete field in add form? – Andrus Oct 30 '11 at 13:11
  • code sample in answer sample calls `grid.jqGrid('getGridParam', 'selrow');` two times – Andrus Oct 30 '11 at 15:36
  • @Andrus: I can't say you what way is *the best* one. It can depend on many parameters. jQuery UI Autocomplete `source` provide you many possibilities how you can get the data from the server, so you should choose yourself which way is *the best for your environment*. I just described two ways which both can be good implemented. – Oleg Oct 30 '11 at 15:49
  • Thank you very much. I tried afterShowForm but this allows to add only single row without closing add form and does not provide way to cancel add command if there is no current row. I updated question. – Andrus Oct 30 '11 at 15:51
  • 1
    @Andrus: In [the next demo](http://www.ok-soft-gmbh.com/jqGrid/InitAddFormDataBasedOnSelectedRow1.htm) you can see that I see no problem with adding of multiple rows without closing the form. If you want to deny Add form dialog you can use `beforeInitData` which returns `false` value (see my last demo). – Oleg Oct 31 '11 at 11:37
  • thank you very much. I asked about adding multiple rows using afterShowForm as shown in question update. In this case multiple rows with initial value cannot added since afterShowForm fires only once if form is initially created, not before every new row adding. afterShowForm is required to to initialize foreign key autocomplete columns. In this case two columns: invisible foreign key and visible name column needs to be initialized. – Andrus Oct 31 '11 at 15:46