Row-level Edit Permission

Using actions column in the earlier example, it’s now possible to specify row-level edit permission for editing using additional javascript.

The Javascript used here hides the delete and edit buttons based condition that when status equals to “Shipped”. The example shown is for demo purpose only. It’s NOT 100% secure. Programmer should still check permission on the server side.

For simpler condition without using Javascript, you can consider set_edit_condition() method.

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
// Always include namespace and conf.php on TOP of the script.
use phpCtrl\C_DataGrid;
require_once("/file/path/to/conf.php");  

// note this should not replace database role-based or user-based permissions.
$onGridLoadComplete = <<<ONGRIDLOADCOMPLETE
function(status, rowid)
{
    var ids = jQuery("#orders").jqGrid('getDataIDs');
    for (var i = 0; i < ids.length; i++)
    {
        var rowId = ids[i];
        var rowData = jQuery('#orders').jqGrid ('getRowData', rowId);

        if($("#orders").jqGrid("getCell", rowId, "status") == "Shipped"){
            $("#orders").jqGrid("setCell", rowId, "actions", " zzz ", {"display":"none"}); // not possible to set value for virtual column
        }
    }
}
ONGRIDLOADCOMPLETE
;

$dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
$dg->set_col_hidden('comments');

$dg->add_column("actions", array('name'=>'actions',
    'index'=>'actions',
    'width'=>'80',
    'formatter'=>'actions',
    'formatoptions'=>array('keys'=>true)),'Actions');
$dg->set_grid_property(array('onSelectRow'=>'')); // remove onSelect event
$dg->add_event("jqGridLoadComplete", $onGridLoadComplete);
$dg->enable_edit('INLINE');

$dg -> display();

See Live Example!