【问题标题】:HTML Table columns inserted with append() not giving expected behaviour使用 append() 插入的 HTML 表列没有给出预期的行为
【发布时间】:2021-04-03 05:35:19
【问题描述】:

我正在尝试制作一个交互式表格网格,当您调整边缘大小时,其他表格也将调整大小。到目前为止,似乎一切正常,直到您必须单击按钮才能添加新列。

预期行为:

如果您尝试拖动前两列的边缘,它们会完美运行。但是,如果您通过单击按钮添加新列,则该列将添加到表中,但与其他两个的行为不匹配。如何使用按钮添加所有列以遵循相同的行为?

$(document).ready(function() {
  AddListeners()

  function AddListeners() {
    var thElm;
    var startOffsetX;
    var startOffsetY;

    Array.prototype.forEach.call(
      document.querySelectorAll("table th"),
      function(th) {
        th.style.position = 'relative';

        var grip = document.createElement('div');
        grip.innerHTML = " ";
        grip.style.top = 0;
        grip.style.right = 0;
        grip.style.bottom = 0;
        grip.style.width = '5px';
        grip.style.position = 'absolute';
        grip.style.cursor = 'col-resize';
        grip.addEventListener('mousedown', function(e) {
          thElm = th;
          startOffsetX = th.offsetWidth - e.pageX;
          startOffsetY = th.offsetHeight - e.pageY;
        });

        th.appendChild(grip);
      });

    document.addEventListener('mousemove', function(e) {
      if (thElm) {
        thElm.style.width = startOffsetX + e.pageX + 'px';
        thElm.style.height = startOffsetY + e.pageY + 'px';
      }
    });

    document.addEventListener('mouseup', function() {
      thElm = undefined;
    });
  };
})

var iter = 2;

function AddColumn() {
  var myform = $('#myTable');
  myform.find('tr').each(function() {
    var trow = $(this);
    iter += 1;
    if (trow.index() === 0) {
      trow.append('<th><div style="top: 0px; right: 0px; bottom: 0px; width: 5px; position: absolute; cursor: col-resize;">&nbsp;</div>th ' + iter + '</th>');
    } else {
      trow.append('<td>th ' + iter + '</td>');
    }
  });
}
table {
  border-width: 1px;
  border-style: solid;
  border-color: black;
  border-collapse: collapse;
}

table td {
  border-width: 1px;
  border-style: solid;
  border-color: black;
}

table th {
  border-width: 1px;
  border-style: solid;
  border-color: black;
  background-color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a onclick="AddColumn()">Add column</a>

<table id="myTable">
  <thead>
    <tr>
      <th>th 1</th>
      <th>th 2</th>
    </tr>
  </thead>
</table>

【问题讨论】:

    标签: javascript html css-tables


    【解决方案1】:

    您的代码不起作用,因为事件侦听器仅在调用 $(document).ready 的回调时附加到最初的两列。一个简单的解决方案是调用 AddListeners()每次插入新列时,如下图:

    function AddListeners() {
        var thElm;
        var startOffsetX;
        var startOffsetY;
    
        Array.prototype.forEach.call(
          document.querySelectorAll("table th"),
          function(th) {
            th.style.position = 'relative';
    
            var grip = document.createElement('div');
            grip.innerHTML = "&nbsp;";
            grip.style.top = 0;
            grip.style.right = 0;
            grip.style.bottom = 0;
            grip.style.width = '5px';
            grip.style.position = 'absolute';
            grip.style.cursor = 'col-resize';
            grip.addEventListener('mousedown', function(e) {
              thElm = th;
              startOffsetX = th.offsetWidth - e.pageX;
              startOffsetY = th.offsetHeight - e.pageY;
            });
    
            th.appendChild(grip);
          });
    
        document.addEventListener('mousemove', function(e) {
          if (thElm) {
            thElm.style.width = startOffsetX + e.pageX + 'px';
            thElm.style.height = startOffsetY + e.pageY + 'px';
          }
        });
    
        document.addEventListener('mouseup', function() {
          thElm = undefined;
        });
      };
    
    $(document).ready(function() {
      AddListeners();
    
      
    })
    
    var iter = 2;
    
    function AddColumn() {
      var myform = $('#myTable');
      myform.find('tr').each(function() {
        var trow = $(this);
        iter += 1;
        if (trow.index() === 0) {
          trow.append('<th><div style="top: 0px; right: 0px; bottom: 0px; width: 5px; position: absolute; cursor: col-resize;">&nbsp;</div>th ' + iter + '</th>');
        } else {
          trow.append('<td>th ' + iter + '</td>');
        }
      });
      AddListeners();
    }
    table {
      border-width: 1px;
      border-style: solid;
      border-color: black;
      border-collapse: collapse;
    }
    
    table td {
      border-width: 1px;
      border-style: solid;
      border-color: black;
    }
    
    table th {
      border-width: 1px;
      border-style: solid;
      border-color: black;
      background-color: green;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <a onclick="AddColumn()">Add column</a>
    
    <table id="myTable">
      <thead>
        <tr>
          <th>th 1</th>
          <th>th 2</th>
        </tr>
      </thead>
    </table>

    这个解决方案很丑陋,因为它会重做侦听器为早期存在的列添加。更好的解决方案是要求您将每列的事件附加过程与 AddListeners() 中的回调分离,并分别在 Array.prototype.forEach.callAddColumn() 中调用它。这是另一个问题。

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2018-09-18
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多