0

I'm in the situation I need to save the state op the grid after re-sizing, reordering, hiding columns.

All works well, except that the column filters (drop-downs, text field, date-picker) are not restored in the correct column after the column with the filter in is moved (collumnchooser).

Using the following JavaScript, I can restore most what I need (size, order and visibility of the columns), but restoring the filters in the right columns doesn't work. (Filters appear several columns after where they should.

var listName = jQuery('#list').jqGrid('getGridParam', 'customName');

var colModel = LoadColumnModel(listName);
var perm = jQuery.cookies.get(listName + '_list_perm');
var rowNumber = jQuery.cookies.get(listName + '_list_rowNumber');

if (colModel) {
    var grid = jQuery('#list');
    for (var i = 0; i < colModel.length; i++) {
        var column = colModel[i];
        if (column.hidden) {
            grid.jqGrid('hideCol', column.name);
        };
        ***//I hoped next line would do the trick, but it didn't :(***
        if (column.search && column.searchoptions) {
            grid.jqGrid('setColProp', column.name, { search: true, searchoptions:   column.searchoptions });
        };
    }
    grid.jqGrid('setGridParam', { colModel: colModel });
    loadGrid = false;
    if (rowNumber) {
        grid.jqGrid('setGridParam', { rowNum: rowNumber });
        jQuery('.ui-pg-selbox').val(rowNumber);
    }
    grid.trigger('reloadGrid');
    if (perm) {
        grid.jqGrid("remapColumns", perm, true);
    }
}

Anyone has a clue?

Steven
  • 1,444
  • 17
  • 23

1 Answers1

1

You should don't set searchoptions in the grid. instead of that you can save/restore the postData parameter of jqGrid.

I would recommend you to look at the answer, the answer and this one. It shows how to implement the saving/restoring of the grid state. I used localStorage instead of the cookies because of reasons which I explained in the answer.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks for the links to all the abundant information, but I don't need the postData (which doesn't contain much info when nothing is selected), I only need to appear the filters in the right column after retoring the grid and collumns have been reordered/hidden. At this moment I don't need the value of the filters. – Steven Jan 12 '12 at 13:02
  • @Steven: Look at the `postData` in case of searching. It contains the full information about the searching filters. Look at the demo from [the answer](http://stackoverflow.com/a/4977896/315935) for example. What you means about the "filters"? It can be interpret in many ways. Do you use advanced searching or `filterToolbar`? Any kind of searching are the filters which are applied of the grid. If you need refill of the searching toolbar look at [another answer](http://stackoverflow.com/a/6884755/315935). – Oleg Jan 12 '12 at 13:26
  • Thanks for your swift answer, and sorry for not pointing out my problem exactly. I indeed mean the filtertoolbar wich appears below the column header and above the grid data. I use for many columns a "dropdown-filter". But my problem is not restoring the selected values after searching, but simply restoring the filter location in the right column in the filtertoolbar. At this moment, when I rearange collumns in the collumchooser, changen column order (columnindex) or hiding them. The filters won't appear on their approriate column. I look into your example if I can find a solution in there. – Steven Jan 12 '12 at 14:26
  • @Steven: It should be a bug in your implementation. Try to reproduce the same on [the demo](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage1.htm) from [the old answer](http://stackoverflow.com/a/8436273/315935) which I referenced in my answer. You can hide some columns, change the column order, set some filters and so on then just reload the page or open it one more time. You will see absolute the same data like before. No problem with the position in the filter toolbar I can see. – Oleg Jan 12 '12 at 14:35
  • Yes that's exactly what I need, but I fail to refactor my mechanism into yours, since it's a complete different approach. BTW, i think there's a minor bug into your grid too, when rearranging the columns through the columnchooser, and immediatelly after that leaving the page, the state as defined in the collumnchooser is not restored. – Steven Jan 12 '12 at 14:50
  • @Steven: Thanks. The problem should be fixed [here](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage1_.htm). I don't recommend you to use cookies and use `localStorage`. I can't help you with refactorring of your current solution. I shows you just my demo which works (at least after the bug fix). – Oleg Jan 12 '12 at 15:10
  • You saved my day, I threw away three days of research and implemented your approach, with some adaptions and now the grid is restored as it should! The example at http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage1_.htm guided me to a solution. – Steven Jan 12 '12 at 15:40
  • One more little question. Can the number of the visible rows (rowNum) also be restored state, I tried to add it, but I think it should be called before the grid loading. – Steven Jan 12 '12 at 16:21
  • @Steven: It's good idea! How you can see from [the modified version of the demo](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage1_.htm) you can implement this very easy in the way which I suggested. – Oleg Jan 12 '12 at 18:19
  • One more question, I encounterd a bug while using your approach and it appears that it is in your example too at the modified version of the demo, mentioned here above, is while trying to sort on the date field, nothing happens because of an error : Uncaught TypeError: Cannot read property 'el' of undefined. I hav no idea how to solve this. – Steven Jan 18 '12 at 09:15
  • @Steven: Could you describe the exact scenario to reproduce the problem? Which demo exactly you used and in which browser? I made some attempts, but I can't reproduce any problem with the sorting by date in [the last demo](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage1_.htm). – Oleg Jan 18 '12 at 10:07
  • I just managaged to reprocude the bug, and build a work-around 1 click the reset grid button, 2 refresh the page, 3 try to sort the date column. – Steven Jan 18 '12 at 12:45
  • I made a quick work-around, by adding the following code to the beginning of the. saveColumnState function. if (perm.length < 1) { var myColumns = new Array(); var index = 0; while (index<=cm.length) { myColumns[index] = index; index++; } perm = myColumns; } It appears the permutation is only set correctly when the collumn chooser is used, implementing the above code created the right permutation state – Steven Jan 18 '12 at 12:52
  • @Steven: Thank you! I could reproduce the problem and found the error. I fixed [the demo](http://www.ok-soft-gmbh.com/jqGrid/ColumnChooserAndLocalStorage1_.htm). The problem was that `remapColumns` was called with empty array `myColumnsState.permutation`. I changed just the line `if (isColState)` to `if (isColState && myColumnsState.permutation.length > 0) {` to call `remapColumns` only if `myColumnsState.permutation` is not empty array. – Oleg Jan 18 '12 at 13:40