ymakux
ymakux

Reputation: 3485

Jqgrid - another way to call onSelectRow

Normally we call onSelectRow function like:

$("#imports_grid").jqGrid({
    // jqGrid settings
    url: url,
    datatype: "json",
    colNames: cnames,
    colModel: cmodel,
    onSelectRow: function() {
      // Code
    },
});

My PHP script outputs jqGrid settings using json_encode() so it looks like

$script = '$("#imports_grid").jqGrid(' . json_encode($js_settings) . ');';
// echo $script;

In this case, onSelectRow's function does not work.

Is there an alternative method to fire function inside onSelectRow?

Upvotes: 0

Views: 940

Answers (1)

Oleg
Oleg

Reputation: 221997

You don't need to mix HTML code and JavaScript code inside of PHP script. Instead of that you can place <script> element

<script type="text/javascript" src="theURL.js"></script>

in the PHP script. The JavaScript code you can place in the .JS file.

UPDATED: You can make of cause a mix between inline JavaScript code where you set some global variables and the JS file where you use the variables:

in PHP code you use

<script type="text/javascript">
    var MYGLOBALSETTINGS = {
        cnames: ["Column 1", "Column 2", ...],
        cmodel: [{...}, {...}...],
        url: "myUrl"
    };
</script>
<script type="text/javascript" src="my.js"></script>

and in my.js you just use MYGLOBALSETTINGS.cnames and MYGLOBALSETTINGS.cmodel

$("#imports_grid").jqGrid({
    // jqGrid settings
    url: MYGLOBALSETTINGS.url,
    datatype: "json",
    colNames: MYGLOBALSETTINGS.cnames,
    colModel: MYGLOBALSETTINGS.cmodel,
    onSelectRow: function() {
      // Code
    },
});

Upvotes: 1

Related Questions