rg88
rg88

Reputation: 20977

jqGrid - Custom filtering works but not after reopening a grid in a modal window

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.

Upvotes: 0

Views: 1255

Answers (1)

Oleg
Oleg

Reputation: 221997

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.

Upvotes: 1

Related Questions