【问题标题】:HTML Table Range Selection/Deselection in JavaScriptJavaScript 中的 HTML 表格范围选择/取消选择
【发布时间】:2021-09-06 02:38:18
【问题描述】:

我需要选择和取消选择表格单元格范围的功能。附加的代码运行良好,但它使用 JQuery,而我的项目的其余部分使用纯 JavaScript。 onmousedown、onmouseover 和 onmouseup 是这段代码中使用的三个鼠标事件。
我试图将此 JQuery 代码转换为纯 JavaScript,但未能成功。如果您能在这方面帮助我,我将不胜感激。
谢谢!

var table = $("#table");    

var isMouseDown = false;
var startRowIndex = null;
var startCellIndex = null;

function selectTo(cell) {
    
    var row = cell.parent();    
    var cellIndex = cell.index();
    var rowIndex = row.index();
    
    var rowStart, rowEnd, cellStart, cellEnd;
    
    if (rowIndex < startRowIndex) {
        rowStart = rowIndex;
        rowEnd = startRowIndex;
    } else {
        rowStart = startRowIndex;
        rowEnd = rowIndex;
    }
    
    if (cellIndex < startCellIndex) {
        cellStart = cellIndex;
        cellEnd = startCellIndex;
    } else {
        cellStart = startCellIndex;
        cellEnd = cellIndex;
    }        
    
    for (var i = rowStart; i <= rowEnd; i++) {
        var rowCells = table.find("tr").eq(i).find("td");
        for (var j = cellStart; j <= cellEnd; j++) {
            rowCells.eq(j).addClass("selected");
        }        
    }
}

table.find("td").mousedown(function (e) {
    isMouseDown = true;
    var cell = $(this);

    table.find(".selected").removeClass("selected"); // deselect everything
    
    if (e.shiftKey) {
        selectTo(cell);                
    } else {
        cell.addClass("selected");
        startCellIndex = cell.index();
        startRowIndex = cell.parent().index();
    }
    
    return false; // prevent text selection
})
.mouseover(function () {
    if (!isMouseDown) return;
    table.find(".selected").removeClass("selected");
    selectTo($(this));
})
.bind("selectstart", function () {
    return false;
});

$(document).mouseup(function () {
    isMouseDown = false;
});
table td {
    border: 1px solid #999;
    width: 40px;
    height: 15px;
    margin: 10px;
}

td.selected {
    background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
Click and drag mouse or use shift key to select cells.
<table id="table">

  <tr>
    <td></td><td></td><td></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td></td><td></td>
  </tr>
  <tr>
    <td></td><td></td><td></td><td></td>
  </tr>
  
</table>

</body>

【问题讨论】:

    标签: javascript jquery html-table


    【解决方案1】:

    使用 querySelectorAll 并根据用途使用索引或使用 foreach。

    window.onload = function() {
    
      var table = document.getElementById('table');
      var isMouseDown = false;
      var startRowIndex = null;
      var startColumnIndex = null;
    
      table.querySelectorAll('tr').forEach(function(tr, rowIndex) {
        tr.querySelectorAll('td').forEach(
          function(td, columnIndex) {
            td.addEventListener('mousedown', function() {
              isMouseDown = true;
              startRowIndex = rowIndex;
              startColumnIndex = columnIndex;
              table.querySelectorAll('td').forEach(function(td) {
                td.removeAttribute('class')
              })
              setSelectedInRange(startRowIndex, startColumnIndex, rowIndex, columnIndex)
            })
            td.addEventListener('mouseover', function() {
              if(!isMouseDown) return;
              setSelectedInRange(startRowIndex, startColumnIndex, rowIndex, columnIndex)
            })
          }
        )
      })
    
      document.addEventListener('mouseup', function() {
        isMouseDown = false;
      })
    }
    
    function setSelectedInRange(rowStart, columnStart, rowEnd, columnEnd) {
      var table = document.getElementById('table');
      table.querySelectorAll('td').forEach(function(td) {
        td.removeAttribute('class')
      })
      if(rowStart > rowEnd) {
        var temp = rowStart;
        rowStart = rowEnd;
        rowEnd = temp;
      }
      if(columnStart > columnEnd) {
        var temp = columnStart;
        columnStart = columnEnd;
        columnEnd = temp;
      }
      var rows = table.querySelectorAll('tr');
      for(var i = rowStart; i <= rowEnd; i++) {
        var currentRow = rows[i];
        var columns = currentRow.querySelectorAll('td');
        for(var j = columnStart; j <= columnEnd; j++) {
          columns[j].setAttribute('class', 'selected')
        }
      }
    }
    

    【讨论】:

    • 谢谢!我没有在 setSelectedInRange 函数中使用像 tempRow 这样的变量。我仍然不清楚它在代码中的逻辑。你能帮我理解 tempRow 变量的作用吗?
    • 只是用来颠倒开始和结束,名字比较混乱,我改成temp,更准确
    • 感谢您的帮助!
    猜你喜欢
    • 2016-10-10
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 2017-03-17
    相关资源
    最近更新 更多