Skip to content

Grid Events

Grid Events

For advance solutions, We are not limited to single table operations. We often need to update several tables and execute extra business cases like sending an email or soft delete a record.

We can have our own code-behind implementation for ADD, UPDATE or DELETE operations.

The on_insert takes 3 params (function-name, class-object or null-if-global-func, continue-default-grid-operation)

If you pass last argument as true, functions will act as a filter and insert/update in ->table will be performed by grid after your function. If last argument is set to false, only your function handler will be executed and grid's internal implementation will be ignored.

$e["on_insert"] = array("add_client", null, false);
$e["on_update"] = array("update_client", null, false);
$e["on_delete"] = array("delete_client", null, true);

// return last inserted id for further working
$e["on_after_insert"] = array("after_insert", null, true);
$e["on_after_update"] = array("after_update", null, true);

// invoked to filter data before displaying on grid
$e["on_data_display"] = array("filter_display", null, true);

// ...


In each callbacks, $data is passed to function which contains all posted data. We can print_r() it for further help.

function add_client($data)
    global $grid;
    $grid->execute_query("INSERT INTO clients
                    VALUES (null,'{$data["params"]["name"]}'

If the 3rd argument is true, the function will behave as a data filter and the final update will be done by grid code. For e.g.

$e["on_update"] = array("update_client", null, true);
function update_client(&$data)
    // set current date time internally
    $data["params"]["reg_date"] = date("Y-m-d H:i:s");

If the 3rd argument is false, the function will be executed and grid's implementation will be skipped. In that case, the callback for on_insert and on_update should echo JSON, e.g.

// if you make it 3rd param to false, then it should return json data
// e.g. $e["on_insert"] = array("add_client", null, false);

function add_client($data)
    // ... custom code to make $sql

    global $grid; // where $grid = new jqgrid(...);

    $insert_id = $grid->execute_query($sql,false,"insert_id");

    if (intval($insert_id)>0)
        $res = array("id" => $insert_id, "success" => true);
        $res = array("id" => 0, "success" => false);

    echo json_encode($res);

Inside callback functions, you can check whether $data variables have all such keys. Following will print $data in error msg. You can debug all data to see the issue.

You can also put phpgrid_error(mysql_error()); before die statement.

function update_client($data)

    // ...

You can also write you custom function for data export (see export-custom.php) $e["on_export"] = array("do_export", null);

// custom on_export callback function
function custom_export($param)
    $sql = $param["sql"]; // the SQL statement for export
    $grid = $param["grid"]; // the complete grid object reference

    if ($grid->options["export"]["format"] == "xls")
        // excel generate code goes here
    else if ($grid->options["export"]["format"] == "pdf")
        // pdf generate code goes here

To use custom SQL for search operations on particular field, you can use on_select event.

$e["on_select"] = array("custom_select","");

function custom_select($d)
    // search params
    $search_str = $this->strip($d["param"]['filters']);
    $search_arr = json_decode($search_str,true);
    $gopr = $search_arr['groupOp'];
    $rule = $search_arr['rules'][0];

    // sort by params
    $sidx = $d["param"]['sidx']; // get index row - i.e. user click to sort
    $sord = $d["param"]['sord']; // get the direction

    if ($rule["field"] == "name")
        $d["sql"] = "select * from clients WHERE name like '%{$rule["data"]}%' ORDER BY $sidx $sord";
        $d["sql_count"] = "select count(*) as c from clients";

You can also set Client side event handlers (e.g. on row select)

Step1: Set JS event handler

// just set the JS function name (should exist)
$e["js_on_select_row"] = "do_onselect";

Step2: Define JS event handler (where 'list1' is grid id and 'company' is field name to load)

function do_onselect(id)
    var rd = jQuery('#list1').jqGrid('getCell', id, 'company'); // where invdate is column name
Company: <span id="span_extra">Not Selected</span>


^ Top