jqgrid - 在函数内启用shift键

时间:2014-07-31 02:29:22

标签: jquery jqgrid

我有一个jqgrid。在我的网格上我使用脚本tp启用shift键来选择一系列选择。我使用了this answer

奥列格的。我以这种方式使用它:

     beforeSelectRow: function (rowid, e) {

                     return shiftSelect(rowid, e);
                    },



function shiftSelect(rowid, e) {
    console.log(rowid);
    var $this = $(this), rows = this.rows,
    // get id of the previous selected row
    startId = $this.jqGrid('getGridParam', 'selrow'),
    startRow, endRow, iStart, iEnd, i, rowidIndex;

if (!e.ctrlKey && !e.shiftKey) {
    $this.jqGrid('resetSelection');
} else if (startId && e.shiftKey) {
    $this.jqGrid('resetSelection');

    // get DOM elements of the previous selected and the currect selected rows
    startRow = rows.namedItem(startId);
    endRow = rows.namedItem(rowid);
    if (startRow && endRow) {
        // get min and max from the indexes of the previous selected
        // and the currect selected rows 
        iStart = Math.min(startRow.rowIndex, endRow.rowIndex);
        rowidIndex = endRow.rowIndex;
        iEnd = Math.max(startRow.rowIndex, rowidIndex);
        for (i = iStart; i <= iEnd; i++) {
            // the row with rowid will be selected by jqGrid, so:
            if (i != rowidIndex) {
                $this.jqGrid('setSelection', rows[i].id, false);
            }
        }
    }

    // clear text selection
    if(document.selection && document.selection.empty) {
        document.selection.empty();
    } else if(window.getSelection) {
        window.getSelection().removeAllRanges();
    }
}
  return true;  
}

我遇到的问题是:当我点击某行时出现此错误:Uncaught TypeError: Cannot read property 'frozenColumns' of undefined

但是当我把这个函数放在beforeSelectRow中时,一切都运行正常。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

以下是答案:

beforeSelectRow: shiftSelect,

var shiftSelect = function (rowid, e) {     

            var $this = $(this), rows = this.rows,
            // get id of the previous selected row
            startId = $this.jqGrid('getGridParam', 'selrow'),
            startRow, endRow, iStart, iEnd, i, rowidIndex;

        if (!e.ctrlKey && !e.shiftKey) {
            $this.jqGrid('resetSelection');
        } else if (startId && e.shiftKey) {
            $this.jqGrid('resetSelection');

            // get DOM elements of the previous selected and the currect selected rows
            startRow = rows.namedItem(startId);
            endRow = rows.namedItem(rowid);
            if (startRow && endRow) {
                // get min and max from the indexes of the previous selected
                // and the currect selected rows 
                iStart = Math.min(startRow.rowIndex, endRow.rowIndex);
                rowidIndex = endRow.rowIndex;
                iEnd = Math.max(startRow.rowIndex, rowidIndex);
                for (i = iStart; i <= iEnd; i++) {
                    // the row with rowid will be selected by jqGrid, so:
                    if (i != rowidIndex) {
                        $this.jqGrid('setSelection', rows[i].id, false);
                    }
                }
            }

            // clear text selection
            if(document.selection && document.selection.empty) {
                document.selection.empty();
            } else if(window.getSelection) {
                window.getSelection().removeAllRanges();
            }
        }
          return true;  

    };