Custom Event Handler

phpGrid supports custom event handler using add_event() method. The event handlers are essentially JavaScript functions so they must be enclosed with PHP heredoc syntax. This is a very powerful function once you are familiar with events.

Below are the events used in this example:

  • jqGridSelectRow
  • jqGridrowattr
  • jqGridAddEditAfterSubmit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");

$onSelectRow = <<<ONSELECTROW
function(status, rowid)
{
    alert('event 1');
    console.log(rowid);
    console.log(status);
}
ONSELECTROW
;

$onSelectRow2 = <<<ONSELECTROW2
function(status, rowid)
{
    alert('event 2');
    console.log("here");
}
ONSELECTROW2
;

$rowattr = <<<ROWATTR
function (rowData, inputRowData)
{
    return rowData.status === "OnHold" ? {style: "background-color:blue"} : {};
}
ROWATTR
;

// post data another page after submit
$afterSubmit = <<<AFTERSUBMIT
function (event, status, postData)
{
    console.log(postData);
    alert(postData.customerNumber);
    $.ajax({ url: '/my/site',
        data: {custNum: postData.customerNumber}, // replace customerNumber with your own field name
        type: 'post',
        success: function(output) {
                    alert(output);
                }
        });
}
AFTERSUBMIT
;

$dg->add_event("jqGridSelectRow", $onSelectRow);
$dg->add_event("jqGridSelectRow", $onSelectRow2);
$dg->add_event("jqGridrowattr", $rowattr);
$dg->add_event("jqGridAddEditAfterSubmit", $afterSubmit);
$dg->enable_edit('FORM');
$dg -> display();

See Live Example! (click on any row to trigger event)

No related content found.

Tags: