代码之家  ›  专栏  ›  技术社区  ›  MVS

表拖动器的基本实现

  •  0
  • MVS  · 技术社区  · 6 年前

    我正在尝试实现table dragger(basic),但无法理解如何完成它。

    以下是我的代码:

    <!DOCTYPE html>
    <html>
    <head>
      <title>draggable</title>
      <script src="/Desktop/table-dragger.min.js"></script>
      <script type="text/javascript">
    
        var el = document.getElementById('table');
    
      var dragger = tableDragger(el, {
      mode: 'row',
      dragHandler: '.handle',
      onlyBody: true,
      animation: 300
    });
    dragger.on('drop',function(from, to){
      console(from);
      console(to);
    });
    
      </script>
    </head>
    <body>
    <table id="table">
    
        <thead>
    
        <tr>
    
          <th class='handle'>Header 1</th>
    
          <th class='handle'>Header 2</th>
    
          <th class='handle'>Header 3</th>
    
        </tr>
    
        </thead>
    
        <tbody>
    
        <tr>
    
          <td>Cell 1</td>
    
          <td>Cell 2</td>
    
          <td>Cell 3</td>
        </tr>
    
        </tbody>
      </table>
    </body>
    </html>
    

    2 回复  |  直到 6 年前
        1
  •  2
  •   Muhammad Usman    6 年前

    如果你检查控制台,你一定会看到 JS 错误。像这样的

    table-dragger: el must be TABLE HTMLElement, not [object Null]
    

    这是因为当您尝试使用DOM时,DOM中没有添加任何表

     var el = document.getElementById('table');
    

    table-dragger 抛出错误。

    sctipt body

    像这样的

    <!DOCTYPE html>
    <html>
    <head>
      <title>draggable</title>
      <script src="https://cdn.jsdelivr.net/npm/table-dragger@1.0.2/dist/table-dragger.min.js"></script>
     
    </head>
    <body>
    <table id="table">
    
        <thead>
    
        <tr>
    
          <th class='handle'>Header 1</th>
    
          <th class='handle'>Header 2</th>
    
          <th class='handle'>Header 3</th>
    
        </tr>
    
        </thead>
    
        <tbody>
    
        <tr>
    
          <td>Cell 1</td>
    
          <td>Cell 2</td>
    
          <td>Cell 3</td>
        </tr>
    
        </tbody>
      </table>
       <script type="text/javascript">
    
        var el = document.getElementById('table');
    
      var dragger = tableDragger(el, {
      mode: 'row',
      dragHandler: '.handle',
      onlyBody: true,
      animation: 300
    });
    dragger.on('drop',function(from, to){
      console(from);
      console(to);
    });
    </script>
    </body>
    
      
    </html>
        2
  •  1
  •   Parth Lukhi    6 年前

    var dragger = tableDragger(el,{    
     mode: 'row',
    dragHandler: '',
    onlyBody: true,
    animation: 300
    });
    

    并将脚本标记放在</车身>