Skip to content

# Subgrid

### Single Subgrid

Setting `subGrid` to `true` will enable subgrid. When clicking `+` icon on parent grid, it will try to load url defined in `subgridurl`. By default 'rowid' (PK) of parent is passed. `subgridparams` holds comma sep. fields that will be POSTed from parent grid to subgrid. They can be read using $_POST in subgrid.

    $opt["subGrid"] = true;
    $opt["subgridurl"] = "subgrid_detail.php";
    $opt["subgridparams"] = "name,gender,company"; // no spaces b/w column names

On subgrid, data can be fetched and passed in SQL

    $c_id = $_REQUEST["rowid"];
    $g->select_command = "SELECT concat(id,'-',num) as `key`, i.*
                            FROM invlines i WHERE id = $c_id";

For extra params passed from parent other than rowid (e.g. company), we need some persistent storage in session for ajax calls

    if (!empty($_POST["company"]))
        $_SESSION["company"] = $_POST['company'];
    $company = $_SESSION['company'];

### Resources

- Sample Code
- See Live Demo
- You can check this demo in archive `demos/master-detail/subgrid.php`

### Multiple Subgrid at same level

To define multiple subgrid at same level, just render 2 grids in detail grid page. Rest process will be same as above subgrid.

    $g = new jqgrid();
    // ...
    $out1 = $g->render('list1');

    $g = new jqgrid();
    // ...
    $out2 = $g->render('list2');

    echo "<fieldset><legend>First Grid</legend>$out</fieldset>";
    echo "<fieldset><legend>Second Grid</legend>$out2</fieldset>";

### Resources

- Parent Grid Code
- Detail Grid Code
- See Live Demo
- You can check this demo in archive `demos/master-detail/multi-subgrid.php`