How to add record to detail grid with specified values from master grid populating the add form

QuestionsHow to add record to detail grid with specified values from master grid populating the add form
Steve Christiansen asked 11 years ago

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!

4 Answers
Abu Ghufran answered 11 years ago

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

Steve Christiansen answered 11 years ago

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?

Abu Ghufran answered 11 years ago

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

Steve Christiansen answered 11 years ago

Abu, thanks so much! Changing lname_subgrid to lname did the trick.

Your Answer

16 + 16 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?