views:

612

answers:

4

My application users asked if it were possible for pages that contain a jqGrid to remember the filter, sort order and current page of the grid (because when they click a grid item to carry out a task and then go back to it they'd like it to be "as they left it")

Cookies seem to be the way forward, but how to get the page to load these and set them in the grid before it makes its first data request is a little beyond me at this stage.

Does anyone have any experience with this kind of thing with jqGrid? Thanks!

A: 

If the user logs in, you might be able to make AJAX requests back to the server when these parameters change. You could then save the settings, either to database (to make them permanent), to session (to persist them temporarily), or both (for performance reasons).

In any case, with the parameters stored server-side, you could just send them to the page when it is requested.

Justin Ethier
Considered doing this, but the problem is that:When a page loads for the first time, it gets a blank filter and sort order and page. How would your server-side data provider know whether that was a "reset the filter and change sort order etc." or a "load my defaults" request? For this reason I thought client side cookies would be better.
Jimbo
Does it matter? If these are the defaults then you should be able to save them on the server and apply them as needed without changing default behavior - or am I missing something?
Justin Ethier
A: 

Hi Jimbo!

I part of work what you need you can implement with respect of jqGridExport and jqGridImport (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:import_methods), but it seems to me you want more as exist out of the box in jqGrid. Some additional features you will have to implement yourself.

Oleg
Thanks Oleg, I hadnt seen this functionality before. It doesnt quite solve my current problem (I have documented my solution below) but will be useful in the future im sure!
Jimbo
I understand that `jqGridExport` and `jqGridImport` gives you too less features, but I don't know nothing more what exist out-of-the-box in jqGrid which supports your requirements. You have to impement it yourself.
Oleg
+2  A: 

PROBLEM SOLVED

I eventually ended up using cookies in javascript to store the sort column, sort order, page number, grid rows and filter details of the grid (using JSON/Javascript cookies - the prefs object)

Save Preferences - Called from $(window).unload(function(){ ... });

var filters = {
    fromDate: $('#fromDateFilter').val(),
    toDate: $('#toDateFilter').val(),
    customer: $('#customerFilter').val()
};

prefs.data = {
    filter: filters,
    scol: $('#list').jqGrid('getGridParam', 'sortname'),
    sord: $('#list').jqGrid('getGridParam', 'sortorder'),
    page: $('#list').jqGrid('getGridParam', 'page'),
    rows: $('#list').jqGrid('getGridParam', 'rowNum')
};

prefs.save();

Load Preferences - Called from $(document).ready(function(){ ... });

var gridprefs = prefs.load();

$('#fromDateFilter').val(gridprefs.filter.fromDate);
$('#toDateFilter').val(gridprefs.filter.toDate);
$('#customerFilter').val(gridprefs.filter.customer);

$('#list').jqGrid('setGridParam', {
    sortname: gridprefs.scol,
    sortorder: gridprefs.sord,
    page: gridprefs.page,
    rowNum: gridprefs.rows
});

// filterGrid method loads the jqGrid postdata with search criteria and re-requests its data
filterGrid();

jqGrid Reference: http://www.secondpersonplural.ca/jqgriddocs/_2eb0fi5wo.htm

BY POPULAR DEMAND - THE FILTERGRID CODE

    function filterGrid() {
        var fields = "";
        var dateFrom = $('#dateFrom').val();
        var dateTo = $('#dateTo').val();

        if (dateFrom != "") fields += (fields.length == 0 ? "" : ",") + createField("shipmentDate", "ge", dateFrom);
        if (dateTo != "") fields += (fields.length == 0 ? "" : ",") + createField("shipmentDate", "le", dateTo);

        var filters = '"{\"groupOp\":\"AND\",\"rules\":[' + fields + ']}"';

        if (fields.length == 0) {
            $("#list").jqGrid('setGridParam', { search: false, postData: { "filters": ""} }).trigger("reloadGrid");
        } else {
            $("#list").jqGrid('setGridParam', { search: true, postData: { "filters": filters} }).trigger("reloadGrid");
        }

    }

    function createField(name, op, data) {
        var field = '{\"field\":\"' + name + '\",\"op\":\"' + op + '\",\"data\":\"' + data + '\"}';
        return field;
    }
Jimbo
I recommend you to think about update of different data on the server side. Will be the filter work? Could you delete the saved filters after updates on the server? The easieast example is the page number. How will look like jqGrid on the client side if at the next time the maximal count of pages will be less then the current page saved in the cookie? Nevertheless I find your implementation good and you go definitively in the correct direction.
Oleg
Hey, Jimbo. I'll have that filterGrid from you if you've got it. +1 on question and answer - exactly what I was looking for.
David M
@David - Updated my answer above to include the filterGrid code - all the best :)
Jimbo
A: 

Hello, I know this is a post older of three months, but is there a way to get the code of the filterGrid method ?

Thank you in advance,

michel

Michel
Updated my answer above to include the filterGrid code - all the best :)
Jimbo