0

I have a custom filtering function, it passes in a custom url and reloads grid data via:

function filter_grid(filter) {
    grid = $(filter).parents('.ui-jqgrid-view').find('.grid');
    /* build the url in here*/
    $(grid).setGridParam({loadonce:false, datatype:'json'});
    jQuery(grid).setGridParam({url:myurl}).trigger("reloadGrid");
    $(grid).setGridParam({loadonce:true});
}

This works fine and filters data as expected. However, I allow the user to open the grid in a modal window and this will also work the first time you open the modal window but if you then close the window and reopen it... filtering fails. I see no errors in console... just the reload never seems to happen. Any suggestions?


Clarification

In a nutshell there is a select menu attached to each grid. You can select an option in that menu and it will reload the grid with the filtered data via the filter_grid function. It reloads by reloading the grid with a new url that passes in some parameters to filter the data.

In the gridComplete event I append a select input element (which I populate later) to each grid on the page. Each one has a class of "filter":

$("#grid1 .ui-jqgrid-titlebar:eq(1)").append("<select id='pdd_user' name='filter_user' class='category_select filter'><option value=''>All</option></select>")

I watch for clicks on the filter class:

$('.filter').live('change', function() {
        filter_grid(this);
    });

That calls the filter_grid function (the one I included above with the first edit of my question), as you can see, and that is what repopulates the grid with the filtered data:

jQuery(grid).setGridParam({url:myurl}).trigger("reloadGrid"); 

I set it to loadonce:false because the grid was set to loadonce:true when it was initially created (for local sorting purposes) so I set it back to loadonce:false, reload the grid with the new url and params, and then set it back to loadonce:true in order to enable local sorting again.

rg88
  • 20,742
  • 18
  • 76
  • 110
  • You posted the code which is difficult to understand. I suppose that even the code is not correct (at least the place where you set `loadonce:true` back). Please describe what should the code do and what happens if "filtering fail". Could you first describe how you implemented the "custom filter"? Do you use deprecated `filterGrid` method of implemented some external filters manually? Please post also the code which calls `filter_grid` function. – Oleg Feb 13 '12 at 20:43
  • @Oleg I have updated this question Oleg, I hope it helps clarify. – rg88 Feb 13 '12 at 22:08

1 Answers1

1

I don't full understand your question, but I hope I could you help to find what's wrong in your code. I think that you should remove any manipulation of loadonce option of jqGrid. It's unneeded and dangerous. To reload the grid with the server data you need just set datatype to 'json' and reload the grid with .trigger("reloadGrid", [{page: 1, current: true}]); (see here for details). The parameter current: true helps to hold selected row if it will be found in the grid after reloading. The parameter page: 1 can help in case if you use data paging. If the user choosed the second page for example and then reloadGrid will be started it will request the server to load also the second page of the filtered results. So one can have empty grid and the page number 2 in the pager. Resetting the page number to 1 either with respect of setGridParam or with the described above parameter of reloadGrid helps in the cases.

Th problem with setting of loadonce: false could be if the reload of grid will be processed before you set loadonce: true. In the case no local data will be filled in the grid. So the option loadonce: true should be permanently set.

I recommend you additionally to read this answer if you uses server side paging with local sorting of data.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • OK, I removed the lines about setting `reloadonce:true` and replaced those with one call like: `jQuery(grid).setGridParam({datatype:'json',url:myurl}).trigger("reloadGrid", [{page:1, current:true}]);` and while this reloads the grid (which it did before) the grid still fails to reload if I do it from a modal window. If I open the modal and use the filter it works fine. If I then close the modal window and reopen it... the grid will no longer reload when I use the filter. – rg88 Feb 13 '12 at 22:49
  • @gaoshan88: You posted no code of "the modal window". It can be that you have the problem not in the parts of code which you posted. Moreover it's still unclear for me how the grid and the filters are connected with the modal. Do you use both the filter and the grid in modal? Do you have only one grid with your custom filter? I ask the last question because the code which you posted contains `id='pdd_user'` for the ` – Oleg Feb 13 '12 at 23:32
  • I updated those fiddles. I hope it's not to much or too messy. – rg88 Feb 14 '12 at 00:38