开发者

How to highlight the last selected row after client-side sorting on 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 hig开发者_开发问答hlighted. My question is, how to keep the selected row being highlighted across data resorting?


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;
    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜