1

I am using jQgrid and I'm building it's content from PHP and send it to the view as a JSON. Below is a snippet of the PHP code I am using currently:

$colFormats[] = [
    'index'     => 'actions',
    'name'      => 'actions',
    'width'     => 70,
    'editable'  => false,
    'formatter' => 'show_btn',
    'sortable'  => false,
    'align'     => 'center'
];

foreach ($classMedata->getFieldNames() as $key => $value) {
    $colFormats[] = [
        'index' => $classMedata->getCollection().'.'.$value,
        'name'  => $value,
        'width' => 100,
    ];
}

return $this->render('IntegrationBundle:api-logs:index.html.twig', [
        'colFormats' => json_encode($colFormats),
        'colNames'   => json_encode($colNames),
]);

This is the Javascript code I have on the view:

<script type="text/javascript">
    var show_btn = function (cellVal, options, rowObject) {
        return '<input style="height:22px;" type="button" value="Show" onclick="" />';
    };

    $(function () {
        $("#grid").jqGrid({
            url: '/sf/api-logs',
            datatype: "json",
            colNames: {{ colNames|raw }},
            colModel: {{ colFormats|raw }},
            width: 980,
            height: 300,
            pager: "#gridpager",
            toppager: true,
            hoverrows: true,
            shrinkToFit: true,
            autowidth: true,
            rownumbers: true,
            viewrecords: true,
            rowList: [10, 20, 50, 100],
            data: [],
            rownumWidth: 50,
            sortable: true,
            jsonReader: {
                root: 'rows',
                page: 'page',
                total: 'total',
                records: 'records',
                cell: '',
                repeatitems: false
            },
            loadComplete: function (data) {
                if (data.records === 0) {
                    $("#load_grid").addClass("info_msg").html($("<span>", {
                        "class": "grid-empty",
                        "text": "No results were found."
                    })).delay(800).fadeIn(400);
                }
            }
        });
    });
</script>

When PHP render the view I can see the proper JS code for colNames and colModel:

colNames: ["Actions", "ID", "Object", ...],
colModel: [{
    "index": "actions",
    "name": "actions",
    "width": 70,
    "editable": false,
    "formatter": "show_btn",
    "sortable": false,
    "align": "center"
}, 
{"index": "ApiLogs.id", "name": "id", "width": 100}, 
{"index": "ApiLogs.dataObject","name": "dataObject","width": 100}, ...

But instead of see the button rendered on the colum Actions I see a undefined word. I am not sure where is my mistake here. Can any help?

I have read the docs and also this post and I think I am doing it right but it's not because of the issue explained above.

Community
  • 1
  • 1
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Please, include in all questions about jqGrid the information about **the version** of jqGrid and about **the fork** ([free jqGrid](https://github.com/free-jqgrid/jqGrid), commercial [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old/retro jqGrid in version <=4.7). – Oleg May 03 '17 at 17:06

1 Answers1

1

The most comfortable form of formatter is the string form like formatter: "myFormatter". You know "predefined formatters" like formatter: "integer", formatter: "date" and so on. I suggest you to register your custom formatter as the "predefined formatters" and set it on the backend.

To do this you need just extend the

$.fn.fmatter.myFormatter = function (cellValue, options, rowData, addOrEdit) {
    // the code of formatter (the same as the custom formatter)
    return '<input style="height:22px;" type="button" value="Show" />';
};
$.fn.fmatter.myFormatter.unformat = function (cellValue, options, elem) {
    // the code of unformatter, like
    // return $(elem).text();
};
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • What about if I want to use a parameter from the grid for an action? Let's said the `ApiLogs.id` you're seeing on the response? can you add this to your answer please? – ReynierPM May 03 '17 at 17:10
  • @ReynierPM: I'm not sure which parameter you mean. Moreover, it's difficult to answer on questions if I don't know the version and the fork of jqGrid, which you use. – Oleg May 03 '17 at 17:14
  • I am using jQgrid 5.1.0 and I am talking about the `id` on each `tr`. I could get it thru jQuery but maybe there is an easy way to get it. – ReynierPM May 03 '17 at 17:16
  • @ReynierPM: the last version of "jqGrid" is 4.7. After version 4.7 the development is spitted to "free jqGrid" product (which I develop, and which is available for free like old version of jqGrid) and commercial "Guriddo jqGrid JS", which price and licence can be found [here](http://guriddo.net/?page_id=103334). 5.1.0 is "Guriddo jqGrid JS". Both products have *different possibilities*. If you just need to access to rowid inside of formatter, you can use `rowId` property of `options` (see [here](https://github.com/free-jqgrid/jqGrid/blob/v4.14.0/ts/free-jqgrid.d.ts#L636-L642)). – Oleg May 03 '17 at 17:24
  • Well then I am using Guriddo jQgrid since it's the version 5.1.0 though I am not sure how to use the `rowId` from whithin the `myFormatter`, that's the example I am looking for. – ReynierPM May 03 '17 at 17:28
  • 1
    @ReynierPM: `myFormatter` has `options` parameter (like the function `show_btn`, which you use). The `rowId` property of `options` (`options.rowId`) is the rowid which you need to have. – Oleg May 03 '17 at 17:31
  • Thanks, but I get it from `rowData.id` and it's working, is that correct? o.O – ReynierPM May 03 '17 at 17:32
  • @ReynierPM: It could be the same, but it could be different too. `options.rowId` is safe. For example, if you have more as one grid on the page or if you use grid as subgrid or in some other cases it's recommended to add `idPrefix` parameter to every grid on the page and to use different values of `idPrefix`. In the case the value of `id` attribute of `` will be build from the value of `idPrefix` and the `id` from your input data. `options.rowId` will be real rowid and `rowData.id` will be without the prefix. There are more important differences depend on the format of input data. – Oleg May 03 '17 at 17:37
  • @ReynierPM: For example, if you use `datatype: "xml"`, then `rowData` will be XML node. If you use `jsonReader` with `repeatitems: true` then `rowData` will be array. By the way free jqGrid fork has `options.rowData`, where `options.rowData` **always** parsed object with named properties (`options.rowData.coumnName`). Guriddo don't have `options.rowData`. There are a lot of such differences now between two forks. – Oleg May 03 '17 at 17:41
  • @ReynierPM: You are welcome! I recommend you additionally to use `beforeSelectRow` to detect clicking on the custom button. Look at [the answer](http://stackoverflow.com/a/42601730/315935) or [this one](http://stackoverflow.com/a/40220736/315935). – Oleg May 03 '17 at 19:03