I need to be able to add a new detail record to a detail grid such that certain values from master grid will pre-populate the detail grid add form prior to submit.
Example:
Master and detail have lname field. A record is selected in master grid and the add (+) icon is selected in the detail grid. The add form of the detail grid appears and it should have lname pre-populate with the value of this field in the master grid.
How can I do this?
Thanks!
You can have before this config in subgrid …
$grid["add_options"]["afterShowForm"] = 'function(formid) { …}';
$g->set_options($grid);
In function body, you can fetch the data from master grid selected row:
var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); // get selected row id form master (list1)
var rd = jQuery('#list1').jqGrid('getCell', selr, 'lname'); // get lname value of selected row
jQuery('#lname_subgrid').val(rd); // set subgrid add form value, whatever field name you wish to update
Abu, thanks. This is what I have just before setting the subgrid options.
$grid = array();
…
$grid["add_options"]["beforeInitData"] = "function(formid){ var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); if (!selr) { alert('Please select master record first'); return false; } }";
…
$grid["add_options"]["afterShowForm"] = "function(formid) {var selr = jQuery('#list1').jqGrid('getGridParam','selrow'); var rd = jQuery('#list1').jqGrid('getCell', selr, 'lname'); jQuery('#lname_subgrid').val(rd);}";
$g->set_options($grid);
Also, for the subgrid column in question (lname):
$col = array();
$col["title"] = "Last Name";
$col["name"] = "lname";
$col["editable"] = true; // this column is editable
$col["editrules"] = array("edithidden"=>true);
$col["hidden"] = true;
$cols1[] = $col;
The sub grid add form is not populated with lname. The lname field is in the master grid row that has been selected. Is there something you can see that is not correct?
$grid["add_options"]["afterShowForm"] = "…";
$g->set_options($grid);
In aftershowform callback, check the following:
1. list1 is master grid id
2. lname in 'jqGrid('getCell', selr, 'lname');' is master grid column name
3. lname_subgrid in 'jQuery('#lname_subgrid').val(rd);' is subgrid column name
By your statement, it looks like both parent & subgrid have lname column, so #3, lname_subgrid will change to lname.
Also check that you parent grid id is correct. You can put alert() calls to verify data is coming or not.