Search code examples
javascriptjqueryjqgridfree-jqgrid

"pager: true" vs "pager: #someid" in jqGrid, how to use them properly?


I am playing a little bit with the jqGrid setup and some things aren't clear to me like the proper usage of pager. The idea is to add some custom buttons to the top and bottom bars.

This is the code I have been playing with:

$("#order_logs").jqGrid({
    url: 'api_order_logs',
    datatype: "json",
    colModel: $('#colmodel').data('values'),
    width: 980,
    height: 300,
    pager: true,
    toppager: true,
    hoverrows: true,
    shrinkToFit: true,
    autowidth: true,
    rownumbers: true,
    viewrecords: true,
    rowList: [25, 50, 100],
    data: [],
    rownumWidth: 100,
    iconSet: "fontAwesome",
    gridview: true,
    sortable: {
        options: {
            items: ">th:not(:has(#jqgh_grid_cb,#jqgh_grid_rn,#jqgh_grid_actions),:hidden)"
        }
    },
    jsonReader: {
        root: 'rows',
        page: 'page',
        total: 'total',
        records: 'records',
        cell: '',
        repeatitems: false
    },
    cmTemplate: {autoResizable: true, editable: true},
    autoResizing: {compact: true, resetWidthOrg: true},
    autoresizeOnLoad: true,
    forceClientSorting: true
}).jqGrid('navGrid', '#gridpager', {
    edit: false,
    add: false,
    del: false,
    search: false,
    refresh: true,
    refreshstate: "current",
    cloneToTop: true
}).jqGrid('navButtonAdd', '#gridpager', {
    caption: 'Export',
    title: 'Export',
    buttonicon: 'fa fa-download',
    onClickButton: function () {
        // @todo
    }
}).jqGrid('navButtonAdd', '#gridpager', {
    caption: 'Email',
    title: 'Email',
    buttonicon: 'fa fa-envelope-o',
    onClickButton: function () {
        // @todo
    }
}).jqGrid('navButtonAdd', '#gridpager', {
    caption: 'Print',
    title: 'Print',
    buttonicon: 'fa fa-print',
    onClickButton: function () {
        // @todo
    }
});

With the usage of pager: true the grid display as follow:

selection_002

Meaning no custom buttons at top nor bottom.

With the usage of pager: #gridpager the grid display as follow:

selection_001

Meaning only custom buttons on the bottom bar but not on the top one.

Certainly I am missing something but I can't find what that is. I've been checking some docs here, here and last here but still not clear to me how to deal with this in the right way.

In addition to this if you notice I am trying to use the fontAwesome iconset but images are missing, should I add the CSS file on my templates?

PS: I am using the latest version of jqGrid-free


Solution

  • It's very easy. Which sense is repeating the same value '#gridpager' as jqGrid option, as the parameter of navGrid and navButtonAdd?

    jqGrid can contain maximal two pagers: bottom pager and top pager. The top pager can be created by usage of the option toppager: true. You don't use the option toppager: true. Then the only legal value of the first parameter of navGrid and navButtonAdd will be '#gridpager'. Correct?

    Now, one can use getGridParam method to get any option of jqGrid after the grid is created. For example one can get the value of pager parameter using

    var pagerIdSelector = $("#order_logs").jqGrid("getGridParam", "pager");
    

    Free jqGrid allows to use

    }).jqGrid('navGrid',  { ... });
    

    instead of

    }).jqGrid('navGrid', '#gridpager', { ... });
    

    It tests the type of the first parameter. If the type of the first parameter isn't "string" then it uses $(this).jqGrid("getGridParam", "pager") to get the value.

    Now we can remind about the possibility to use toppager: true to create top pager. In the case jqGrid creates an empty <div> for the top pager, it generates, it assigns unique id for the <div>. Finally jqGrid changes the value of toppager parameter from true to the value like #order_logs_toppager, there the first part of the id (order_logs) is the id of the grid. Free jqGrid allows to use true as the value of pager parameter. In the case one can simplify the HTML and remove unneeded empty div

    <div id="gridpager"><div>
    

    If one uses both pager: true and toppager: true options and uses navGrid or navButtonAdd without pager parameters then jqGrid places the buttons on both pagers. If you need to place some buttons only on one pager then you can use the code like below. If place some buttons on both pagers and then place another buttons on specific pagers:

    var $grid = $("#order_logs");
    
    $grid.jqGrid({
        ...
        pager: true,
        toppager: true,
        ...
    });
    
    // create nav bar and place Refresh button on both pagers
    $grid.jqGrid('navGrid', {
        edit: false,
        add: false,
        del: false,
        search: false,
        refreshstate: "current",
    });
    
    var bottomPagerIdSelector = $grid.jqGrid("getGridParam", "pager"),
        topPagerIdSelector = $grid.jqGrid("getGridParam", "toppager");
    // place Export button only on bottom pager
    // and Email button only on top pager
    $grid.jqGrid('navButtonAdd', bottomPagerIdSelector, {
        caption: 'Export',
        title: 'Export',
        buttonicon: 'fa fa-download',
        onClickButton: function () {
            // @todo
        }
    }).jqGrid('navButtonAdd', topPagerIdSelector, {
        caption: 'Email',
        title: 'Email',
        buttonicon: 'fa fa-envelope-o',
        onClickButton: function () {
            // @todo
        }
    });
    

    The final remarks. You use forceClientSorting: true option, which works only in combination with loadonce: true. jqGrid can sort all the data only it it has all data.

    The value of sortable parameter is wrong. The selectors like jqgh_grid_cb contains "grid" in the middle, which should be the id of the grid. In your case it could be "order_logs" instead of "grid" (jqgh_grid_cb shoule be replaced to jqgh_order_logs_cb).