jqGrid for PHP supports adding javascript from within your PHP script. This is done with the special prefix "js:" before the script.
It is very important to note that there are exceptions to this rule when the following methods are used:
  • setGridEvent
  • setNavEvent
  • setJSCode

placing a "js:" prefix before the code will not run the script. These methods add the "js:" prefix automatically before the code


When should the "js:" prefix be used?

The "js:" prefix should be used when the type definition is function and in the following options - Column Model Options for extending the editing properties,in Formatters to define custom formatter and in Toolbar Filter.

If the javascript code is complex and spans several lines, we recommend using the following syntax:

$mycode = <<<CUSTOMCODE // // here JavaScript code CUSTOMCODE;

Using this syntax will prevent PHP errors with unescaped single and double brackets, which will otherwise cause errors in the PHP script.
You can see an example of this here

setGridEvent

With the setGridEvent method we add client-side javascript events related to the grid. The method accepts two parameters - the first parameter is the valid event name listed here and the second parameter is the javascript code which should begin with function and the parameters which the event accepts.

Let suppose that we want to alert a message with the OrderID when a row in the grid is selected (OrderID is a column in the grid). Using our example the change in the code should be:

<?php ... $onselect = <<<ONSELECTROW function(rowid, selected) { alert(rowid); } ONSELECTROW; $grid->setGridEvent('onSelectRow',$onselect); $grid->renderGrid('#grid','#pager',true, null, null, true,true); $conn = null; ?>

setNavEvent

setNavEvent works in the same way as setGridEvent, but it adds a javascript events to the navigator or/and form-editing modules.

setJSCode
setJSCode adds code written in javascript just after the creation of jqGrid instance. Use this method to emit custom javascript related to your jqGrid instance.