2

The question is almost complete in the title: is there any way, using jquery, to find the element that is handled by a jqGrid object?

To be more precise, I wish to call a reload method on the grid, if one is present on the page. I am using the following code

...
success: function (data) {
    //Check if there is a jqGrid on the page and if present, reloads its data
    var jqGrid = $('.ui-jqgrid');
    if ( jqGrid.length ) {
        //get the grid id. The actual id object is in the form of "gbox_your_grid_id"
        var gridid = "#" + jqGrid.attr('id').substring(5);
        //time to reload
        $(grid).trigger('reloadGrid');
    }
}

but it seems that the reloadGrid method is never called. Any suggestion?

Lorenzo
  • 29,081
  • 49
  • 125
  • 222

2 Answers2

5

It's difficult to suggest any test which will be perfect. You can try for example to search for the "gbox": $('div.ui-jqgrid') - it is a div which contain all components of jqGrid. If $('div.ui-jqgrid').length > 0 then exist at least grid on the page.

You can search for the table element inside of bdiv:

$('div.ui-jqgrid-bdiv table').length > 1

or even for

if ($('div.ui-jqgrid > div.ui-jqgrid-view > div.ui-jqgrid-bdiv > div > table.ui-jqgrid-btable').length > 1) {
    // jqGrid exist
}

(see here for details). Additionally you can test whether such table elements have jqGrid method:

if ($.isFunction($('div.ui-jqgrid > div.ui-jqgrid-view > div.ui-jqgrid-bdiv > div > table.ui-jqgrid-btable').jqGrid)) {
    // jqGrid exist
}
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks. I have edited my question to add further details as per your suggestion. Can you please have a look at it? – Lorenzo Jun 12 '11 at 21:47
  • @Lorenzo: Not exact this. The `$('.ui-jqgrid')` is not a `` element. So you should use at least `var jqGrid = $('div.ui-jqgrid-bdiv table');` or more exact `var jqGrid = $('div.ui-jqgrid > div.ui-jqgrid-view > div.ui-jqgrid-bdiv > div > table.ui-jqgrid-btable');`. You don't need to use and `var gridid` and just call `jqGrid.trigger('reloadGrid',[{page:1}])`. See [here](http://stackoverflow.com/questions/3807623/jqgrid-paging-question/3808014#3808014).
    – Oleg Jun 12 '11 at 21:51
  • @Lorenzo: You can find first the gbox and then get the last part of id to find the table, but to search the table inside the gbox directly like I described in the previous comment seems me better. – Oleg Jun 12 '11 at 21:59
1

I don't know jqGrid itself but I'm pretty sure looking at what it does that it adds custom classes to style elements, so you can most likely identify which class is of interest to you and then use $('.classname') to grab all elements.

Seldaek
  • 40,986
  • 9
  • 97
  • 77