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

前端 未结 1 659
心在旅途
心在旅途 2021-02-06 10:41

I\'ve a jqGrid-based application, and I use loadonce: true in the grid option and sortable: true, sorttype: \'text in the colModel to allo

1条回答
  •  野性不改
    2021-02-06 11:05

    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 讨论(0)
提交回复
热议问题