Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight the last selected row after client-side sorting on jqGrid?

Tags:

jqgrid

I've a jqGrid-based application, and I use loadonce: true in the grid option and sortable: true, sorttype: 'text in the colModel to allow client-side sorting on the data grid. However, I found that, once the data grid is re-sorted, the last selected row will no longer be highlighted. My question is, how to keep the selected row being highlighted across data resorting?

like image 248
William X Avatar asked Jul 30 '11 07:07

William X


1 Answers

I prepared for you the small demo which keeps the row selection. In the demo I rewrote the code of selectionPreserver used in case of reloadGrid usage having additional parameters: $("#list").trigger("reloadGrid", [{current:true}]);. See the answer for details.

The demo saves the current selection inside of the onSortCol event handler and restore it inside of the loadComplete:

onSortCol: function () {
    saveSelection.call(this);
},
loadComplete: function () {
    restoreSelection.call(this);
}

How you see the usage is very simple and you can integrate it in your code. The implementation of the saveSelection and restoreSelection is the following:

var lastSelArrRow = [],
    lastScrollLeft = 0,
    lastSelRow = null,
    saveSelection = function () {
        var $grid = $(this);
        lastSelRow = $grid.jqGrid('getGridParam', 'selrow');
        lastSelArrRow = $grid.jqGrid('getGridParam', 'selrow');
        lastSelArrRow = lastSelArrRow ? $.makeArray(lastSelArrRow) : null;
        lastScrollLeft = this.grid.bDiv.scrollLeft;
    },
    restoreSelection = function () {
        var p = this.p,
            $grid = $(this);

        p.selrow = null;
        p.selarrrow = [];
        if (p.multiselect && lastSelArrRow && lastSelArrRow.length > 0) {
            for (i = 0; i < lastSelArrRow.length; i++) {
                if (lastSelArrRow[i] !== lastSelRow) {
                    $grid.jqGrid("setSelection", lastSelArrRow[i], false);
                }
            }
            lastSelArrRow = [];
        }
        if (lastSelRow) {
            $grid.jqGrid("setSelection", lastSelRow, false);
            lastSelRow = null;
        }
        this.grid.bDiv.scrollLeft = lastScrollLeft;
    };
like image 178
Oleg Avatar answered Sep 27 '22 01:09

Oleg