jqGrid resolve the grid pager ID dynamically?

17,712

You can find the jqGrids existing on the page in many ways. For example you can use $('table.ui-jqgrid-btable') instead of $('div.ui-jqgrid-bdiv table'). Moreover you should not forget that it can be more as one jqGrid on the page in general. I recommend you to write your code so that it will work with many jqGrids of the page even if you currently use only one jqGrid per page.

If you find in any way the table element of jqGrid you can get the DOM element of the first found grid with jqGrids[0]. jqGrid use some extenders of the DOM. It add additional properties grid and p. In every jqGrid method will be checked whether the grid is initialized by verifying that grid property exist. The p property gives you all jqGrid parameters inclusive p.pager. You can create up to two pager on on grid: one on top edge of the grid and another on the bottom (see this for more information). So the code which you need could looks like

var jqGrids = $('table.ui-jqgrid-btable');
if (jqGrid.length > 0) {
    jqGrid.each(function(i) {
        if (this.grid) {
            // one more test for the jqGrid
            // jqGrid[i] is a jqGrid
            if (this.p.toppager) {
                // this.id + '_toppager' is the id of the top pager
            }
            if (this.p.pager) {
                // this.p.pager is the id of the bottom pager
            }
        }
    });
}

To test whether the table element has some customclass class you can use jQuery.hasClass.

UPDATED: In the comment you asked me how to hide or to show the buttons in the navigator bar dynamically. I prepared the demo which demonstrate this:

enter image description here

If one checks the buttons which are placed above the grid the corresponding button from the navigator bar will be hide. Unchecking will show the corresponding button back.

The code just call $('#add_list').hide() or $('#add_list').show() to hide/show the "Add" button. In the example the last part of the id="add_list" is the id of the <table> element used to create the grid. Other standard buttons have the ids starting with the following prefixes: 'edit_', 'view_', 'del_', 'search_', 'refresh_'. More common code which works if the id of the grid has special characters looks as following:

var grid = $("#list"),
    gid = $.jgrid.jqID(grid[0].id);

$('#cbAdd').change(function () {
    var $td = $('#add_' + gid);
    if ($(this).is(':checked')) {
        $td.hide();
    } else {
        $td.show();
    }
});

To find the custom navigator buttons added by navButtonAdd I use title attribute:

$('#cbChooseColumns').change(function () {
    var $td = $(grid[0].p.pager + '_left ' + 'td[title="choose columns"]');
    if ($(this).is(':checked')) {
        $td.hide();
    } else {
        $td.show();
    }
});
Share:
17,712
Lorenzo
Author by

Lorenzo

What should I say here?

Updated on June 11, 2022

Comments

  • Lorenzo
    Lorenzo almost 2 years

    I have 3 simple questions.

    1. I have some code that tells me if a jqGrid object is present in the page:

      //Check if there is a jqGrid on the page and if present, reloads its data ;)
      var jqGrid = $('div.ui-jqgrid-bdiv table');
      if (jqGrid.length) {
          //time to reload
          $(jqGrid).trigger('reloadGrid');
      }
      

      I would like to find the pager ID element if there is one. Is there any way to do this?

    2. Suppose I have a custom class in my jqGrid table:

      <table id="myGrid" runat="server" class="customclass"></table>
      <div id="myGrid_pager" runat="server"></div>
      

      How do I check the presence of customclass inside my jqGrid dynamically?

    EDIT:

    With Oleg help I have been able to code a reconfigPermissions() function that show/hide default Add, Edit and Delete buttons. Here is the function:

    function reconfigPermissions(gridID) {
        var enableRegistry = CanModifyRegistry();
        var ops = ['#add_' + gridID, '#edit_' + gridID, '#del_' + gridID];
        $.each(ops, function (ix, value) {
            var $td = $(value);
            if (enableRegistry === true) {
                $td.show();
            } else {
                $td.hide();
            }
        });
    }
    

    This function get called when the user select another range of dates in a combo box defined elsewhere in the page. The problem is the following: if, when the grid is initially loaded, the user has rights to the default period (selected in the combo box) everything works. You can switch the date range in the combo and the buttons appear and disappear correctly. Unfortunately if the user has no rights on the default period initially selected (so the first creation of the grid has {add: false, edit: false, del: false}) even switching to a period where the user has rights does not add the buttons at all.

    This is the code binded to the combo box change event handler

    $.ajax({
        url: GetBaseWSUrl() + 'MyWebService.asmx/ChangeCurrentPeriod',
        type: "post",
        dataType: "json",
        async: false,
        data: JSON.stringify({ periodID: $(this).val() }),
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            //Check if there is a jqGrid on the page and if present, reloads its data
            var jqGrids = $('div.ui-jqgrid-bdiv table');
            jqGrids.each(function (ix, jqGrid) {
                var gridID = $.jgrid.jqID(jqGrid.id)
                reconfigPermissions(gridID);
                jqGrid.trigger('reloadGrid');
            });
        }
    });
    

    Any suggestion?

  • Lorenzo
    Lorenzo almost 13 years
    @Thanks Oleg. What I really wanted to do is to change visibility of add/edit/delete buttons on the fly (after the grid has been created). Can I do it? If I do, as per in your sample, this.grid.jqGrid('navGrid', this.p.pager, { add: false, edit: false, del: false } ); does not work :(
  • Oleg
    Oleg almost 13 years
    @Lorenzo: Do you want to hide/show "Add", "Edit" or "Delete" button dynamically in the navigation bar?
  • Oleg
    Oleg almost 13 years
    @Lorenzo: Look at the demo. I'll write comment to the code later today.
  • Lorenzo
    Lorenzo almost 13 years
    Actually there is an annoying problem. Please have a look at my question edit. Thanks.
  • Oleg
    Oleg almost 13 years
    @Lorenzo: You don't included the code which define jqGrid and which call . I suppose that your problem is following: you have to use always {add: true, edit: true, del: true} parameters in navGrid. So that all buttons will be created. Immediately after the calling of navGrid you can hide the unneeded buttons calling of reconfigPermissions function.
  • Lorenzo
    Lorenzo almost 13 years
    @Thanks Oleg. That is for sure a suitable solution. Ciao
  • Oleg
    Oleg almost 13 years
    @Lorenzo: You are welcome! If you "play" with buttons from the navigator bar you can consider to disable/enable the buttons depend on selected row. See the demo from the answer. The first row from the demo is "non-editable". Try to select/unselect the second row and look at the "Edit" and "Delete" navigation buttons.
  • santoshM
    santoshM almost 7 years
    @Oleg Could you please answer the question posted on the below link: stackoverflow.com/questions/45813662/…
  • Thomas J Younsi
    Thomas J Younsi about 6 years
    What about the id for the one jQuery("#mygrid").jqGrid('navGrid', '#pager', {cloneToTop: true});
  • Thomas J Younsi
    Thomas J Younsi about 6 years
    sample for top pager after being cloned: var grid = jQuery("#mygrid"); grid.jqGrid('navGrid', '#pager', {cloneToTop: true}); $td = $(grid[0].p.toppager + '_left ' + 'td[title="Add new row"]'); $td.hide(); $td = $(grid[0].p.toppager + '_left ' + 'td[title="Edit selected row"]'); $td.hide(); $td = $(grid[0].p.toppager + '_left ' + 'td[title="Delete selected row"]'); $td.hide(); $td = $(grid[0].p.toppager + '_left ' + 'td[title="Find records"]'); $td.hide();