Javascript SHift单击jqgrid multiselect缺少最后一行

Javascript SHift单击jqgrid multiselect缺少最后一行,javascript,jquery,jqgrid,multi-select,shift,Javascript,Jquery,Jqgrid,Multi Select,Shift,我采用了来自的代码并制作了。尝试单击第一行,然后按住shift键并单击最后一行。如果您注意到这段代码做得很好,除了最后一行之外,您单击的行没有被选中。我一直在为这件事挠头。有人能帮我修改代码,让multiselect也选择最后一行吗 谢谢 如果((shouldSelectRow=id==startID | | shouldSelectRow)){替换为: if ((shouldSelectRow = id == startID || shouldSelectRow) && (id

我采用了来自的代码并制作了。尝试单击第一行,然后按住shift键并单击最后一行。如果您注意到这段代码做得很好,除了最后一行之外,您单击的行没有被选中。我一直在为这件事挠头。有人能帮我修改代码,让multiselect也选择最后一行吗


谢谢

如果((shouldSelectRow=id==startID | | shouldSelectRow)){替换为:

if ((shouldSelectRow = id == startID || shouldSelectRow) && (id != rowid)){

我同意Michael Gendin的观点,即您不应选择id等于
rowid
的行。这是您的主要错误。尽管如此,我还是会重写演示的大部分代码,使用行的DOM元素,而不是枚举网格中的所有行。此外,IE中文本的选择在您当前的代码中是不舒服的e、 因此,我建议将其删除。您发现的经过修改的演示在SelectRow回调之前使用了以下代码:

beforeSelectRow: 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;
}
beforeSelectRow: function (rowid, e) {
                var $this = $(this), rows = this.rows,
                // get id of the previous selected row
                previousId = $this.jqGrid('getGridParam', 'selrow'),
                previousRow, currentRow;

                if (!e.ctrlKey && !e.shiftKey) {
                    _isSideDown = null;                       
                        $this.jqGrid('resetSelection');                       

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


                    // get DOM elements of the previous selected and the currect selected rows
                    previousRow = rows.namedItem(previousId);
                    currentRow = rows.namedItem(rowid);
                    if (previousRow && currentRow) {
                        //Increment
                        if (previousRow.rowIndex < currentRow.rowIndex) {
                            if (_isSideDown == false || _isSideDown == null) {
                                _currentStartSelectRow = previousRow.rowIndex;
                                _currentEndSelectRow = currentRow.rowIndex;
                            }
                            else {
                                _currentEndSelectRow = currentRow.rowIndex;
                            }
                            _isSideDown = true;
                        }
                        //Decrement
                        else {
                            if (_isSideDown == null) {
                                _currentStartSelectRow = currentRow.rowIndex;
                                _currentEndSelectRow = previousRow.rowIndex;
                                _isSideDown = false;
                            }
                            else if (_isSideDown == true) {
                                if (currentRow.rowIndex < _currentStartSelectRow) {
                                    _currentStartSelectRow = currentRow.rowIndex;
                                    _isSideDown = false;
                                }
                                else {
                                    _currentEndSelectRow = currentRow.rowIndex;
                                }
                            }
                            else {
                                _currentStartSelectRow = currentRow.rowIndex;
                            }

                        }

                        for (i = _currentStartSelectRow; i <= _currentEndSelectRow; i++) {
                            // the row with rowid will be selected by jqGrid, so we don't need to select him:
                            if (i != currentRow.rowIndex) {
                                $this.jqGrid('setSelection', rows[i].id, false);
                            }
                        }
                    }

                }
                return true;
            }, 
在选择行之前:函数(rowid,e){
var$this=$(this),rows=this.rows,
//获取上一个选定行的id
startId=$this.jqGrid('getGridParam','selrow'),
startRow、endRow、iStart、iEnd、i、rowidIndex;
如果(!e.ctrlKey&&!e.shiftKey){
$this.jqGrid('resetSelection');
}else if(startId&e.shiftKey){
$this.jqGrid('resetSelection');
//获取上一个选定行和当前选定行的DOM元素
startRow=rows.namedItem(startId);
endRow=rows.namedItem(rowid);
如果(开始和结束行){
//从上一个选定对象的索引中获取最小值和最大值
//和当前选定的行
iStart=Math.min(startRow.rowIndex,endRow.rowIndex);
rowidIndex=endRow.rowIndex;
iEnd=Math.max(startRow.rowIndex,rowidIndex);

对于(i=iStart;i,如Oleg的回答中所述,这里有一个调整后的beforeSelectRow,它进行附加选择

在我的例子中,我们的用户正在选择一组行进行导出,因此额外的选择通常并不意味着他们想要开始新的选择

 beforeSelectRow: 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) {
          //intentionally left here to show differences with
          //Oleg's solution. Just have normal behavior instead.
          //$this.jqGrid('resetSelection');
      } else if (startId && e.shiftKey) {
          //Do not clear existing selections
          //$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);

              // get the rowids of selected rows
              var selected = $this.jqGrid('getGridParam','selarrrow');

              for (i = iStart; i <= iEnd; i++) {
                  // if this row isn't selected, then toggle it.
                  // jqgrid will select the clicked on row, so just ingore it.
                  // note that we still go <= iEnd because we don't know which is start or end.
                  if(selected.indexOf(rows[i].id) < 0 && i != rowidIndex) {
                    // true is to trigger onSelectRow event, which you may not need
                    $this.jqGrid('setSelection', rows[i].id, true);
                  }
              }
          }

          // clear text selection (needed in IE)
          if(document.selection && document.selection.empty) {
              document.selection.empty();
          } else if(window.getSelection) {
              window.getSelection().removeAllRanges();
          }
      }
      return true;
  }
在选择行之前:函数(rowid,e){
var$this=$(this),rows=this.rows,
//获取上一个选定行的id
startId=$this.jqGrid('getGridParam','selrow'),
startRow、endRow、iStart、iEnd、i、rowidIndex;
如果(!e.ctrlKey&&!e.shiftKey){
//故意留在这里以显示与
//奥列格的解决方案。只要有正常的行为。
//$this.jqGrid('resetSelection');
}else if(startId&e.shiftKey){
//不清除现有选择
//$this.jqGrid('resetSelection');
//获取上一个选定对象的DOM元素并
//当前选定的行
startRow=rows.namedItem(startId);
endRow=rows.namedItem(rowid);
如果(开始和结束行){
//从上一个选定对象的索引中获取最小值和最大值
//和当前选定的行
iStart=Math.min(startRow.rowIndex,endRow.rowIndex);
rowidIndex=endRow.rowIndex;
iEnd=Math.max(startRow.rowIndex,rowidIndex);
//获取所选行的rowid
所选变量=$this.jqGrid('getGridParam','selarrrow');

对于(i=iStart;iAdd
$(“#grid”).disableSelection();
要删除恼人的文本选择

Oleg的解决方案并非在所有选择模式下都能工作(向上/向下)。感谢他提供了部分解决方案

我使用以下代码更正此问题:

存储的当前起始行Id和结束行Id需要2个变量。 另一个用于存储所选内容的一侧

var _currentStartSelectRow, _currentEndSelectRow, _isSideDown = null;
beforeSelectRow回调的代码调用:

beforeSelectRow: 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;
}
beforeSelectRow: function (rowid, e) {
                var $this = $(this), rows = this.rows,
                // get id of the previous selected row
                previousId = $this.jqGrid('getGridParam', 'selrow'),
                previousRow, currentRow;

                if (!e.ctrlKey && !e.shiftKey) {
                    _isSideDown = null;                       
                        $this.jqGrid('resetSelection');                       

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


                    // get DOM elements of the previous selected and the currect selected rows
                    previousRow = rows.namedItem(previousId);
                    currentRow = rows.namedItem(rowid);
                    if (previousRow && currentRow) {
                        //Increment
                        if (previousRow.rowIndex < currentRow.rowIndex) {
                            if (_isSideDown == false || _isSideDown == null) {
                                _currentStartSelectRow = previousRow.rowIndex;
                                _currentEndSelectRow = currentRow.rowIndex;
                            }
                            else {
                                _currentEndSelectRow = currentRow.rowIndex;
                            }
                            _isSideDown = true;
                        }
                        //Decrement
                        else {
                            if (_isSideDown == null) {
                                _currentStartSelectRow = currentRow.rowIndex;
                                _currentEndSelectRow = previousRow.rowIndex;
                                _isSideDown = false;
                            }
                            else if (_isSideDown == true) {
                                if (currentRow.rowIndex < _currentStartSelectRow) {
                                    _currentStartSelectRow = currentRow.rowIndex;
                                    _isSideDown = false;
                                }
                                else {
                                    _currentEndSelectRow = currentRow.rowIndex;
                                }
                            }
                            else {
                                _currentStartSelectRow = currentRow.rowIndex;
                            }

                        }

                        for (i = _currentStartSelectRow; i <= _currentEndSelectRow; i++) {
                            // the row with rowid will be selected by jqGrid, so we don't need to select him:
                            if (i != currentRow.rowIndex) {
                                $this.jqGrid('setSelection', rows[i].id, false);
                            }
                        }
                    }

                }
                return true;
            }, 
在选择行之前:函数(rowid,e){
var$this=$(this),rows=this.rows,
//获取上一个选定行的id
previousId=$this.jqGrid('getGridParam','selrow'),
上一行,当前行;
如果(!e.ctrlKey&&!e.shiftKey){
_isSideDown=null;
$this.jqGrid('resetSelection');
}else if(上一个ID和e.shiftKey){
$this.jqGrid('resetSelection');
//获取上一个选定行和当前选定行的DOM元素
previousRow=rows.namedItem(previousId);
currentRow=rows.namedItem(rowid);
如果(上一行和当前行){
//增量
if(previousRow.rowIndex