【问题标题】:JavaScript: how to set drag-and-drop step in tableJavaScript:如何在表格中设置拖放步骤
【发布时间】:2017-08-03 10:30:14
【问题描述】:

我有这样的代码:

<div class="table-area">
  <table>
    <thead>
      <tr>
      <th>someDate</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>someDateVal1</td>
        <td class="data-cell"></td>
        <td class="data-cell"></td>
        <td class="data-cell"></td>
      </tr>
      <tr>
        <td>someDateVal2</td>
        <td class="data-cell"></td>
        <td class="data-cell"></td>
        <td class="data-cell"></td>
      </tr>
    </tbody>
  </table>

  <div class="table-area-selected"
    draggable="true"></div>
</div>

和js:

$(function() {

  var selected = $('.table-area-selected');

  var cell = $('table').find('.data-cell');


  selected.css('width', $(cell[0]).outerWidth() * 2);
  selected.css('height', $(cell[0]).outerHeight());


  selected.css('top', $(cell[0]).position().top);
  selected.css('left', $(cell[0]).position().left);

  $('.table-area-selected').on('dragstart', function(event) {
    console.log('drag', event);
  });

  $('table').on('drop', function(event) {
    var selected = $('.table-area-selected');

    var cell = event.target;

    console.log('drop', event);


    selected.css('width', $(cell).outerWidth() * 2);
    selected.css('height', $(cell).outerHeight());


    selected.css('top', $(cell).position().top);
    selected.css('left', $(cell).position().left);
  });

  $('table').on('dragover', function(event) {
    event.preventDefault();
  });


});

https://plnkr.co/edit/NpRHbgHnUgGfgAOJnSTw?p=preview

是否可以像其他日程插件一样拖动这个项目?像这样:https://dhtmlx.com/docs/products/demoApps/room-reservation-html5-js-php/

因为现在我的矩形是空闲的。我需要在表格网格上设置它的动作:像这样: https://www.screencast.com/t/EXKQwTwTwkb 而不是这个: https://www.screencast.com/t/g6jbP4s9hBX2

可以吗?

【问题讨论】:

    标签: javascript jquery html drag-and-drop


    【解决方案1】:

    在这种情况下,我个人不会使用 HTML 5 拖放,我会选择鼠标事件。

    请注意,我写的是 2 列跨度;如果您希望它更灵活,则需要对其进行调整。

    $(function() {
    
      var isDragging = false;
    
      var $selected = $('.table-area-selected');
    
      var $cells = $('table').find('.data-cell');
      var colSpan = 2;
      var $currentCell = $($cells[0]);
      var cellWidth = $currentCell.outerWidth();
    
      $selected.css('width', cellWidth * colSpan);
      $selected.css('height', $currentCell.outerHeight() - 2); // fiddle factor
      $selected.css('top', $currentCell.position().top);
      $selected.css('left', $currentCell.position().left);
    
      // drag start
      $selected.mousedown(dragStart);
    
      // drag end
      $(window).mouseup(dragEnd);
    
      // drag over cells
      $cells.mouseenter(draggingIntoNewCell);
      $selected.mousemove(draggingInSelectedCell);
    
    
      function dragStart() {
        isDragging = true;
      }
    
      function dragEnd() {
        isDragging = false;
      }
    
      function draggingIntoNewCell() {
        $currentCell = $(this);
        reposition($currentCell);
      }
    
      // find if we've moved into the next column under this selection
      function draggingInSelectedCell(e) {
    
        if (isDragging) {
    
          // find relative position within selection div
          var relativeXPosition = (e.pageX - $(this).offset().left);
    
          if (relativeXPosition > cellWidth) { // moved into next column
            $currentCell = $currentCell.next();
            reposition($currentCell);
          }
        }
      }
    
      function reposition($cell) {
    
        // only reposition if not the last cell in the table (otherwise can't span 2 cols)    
        if (isDragging && $cell.next().hasClass('data-cell')) {
          $selected.css('top', $cell.position().top);
          $selected.css('left', $cell.position().left);
        }
      }
    
    });
    table th,
    table td {
      padding: 8px 40px;
      border: 1px solid #cecece;
      position: relative;
      -moz-user-select: none;
      -webkit-user-select: none;
      -ms-user-select: none;
    }
    
    .table-area-selected {
      position: absolute;
      background: green;
      border: 1px solid blue;
      cursor: pointer;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <script data-require="jquery@*" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    </head>
    
    <body>
      <h1>Hello Plunker!</h1>
    
      <div class="table-area">
        <table>
          <thead>
            <tr>
              <th>someDate</th>
              <th>1</th>
              <th>2</th>
              <th>3</th>
              <th>4</th>
            </tr>
          </thead>
    
          <tbody>
            <tr>
              <td>someDateVal1</td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
            </tr>
            <tr>
              <td>someDateVal2</td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
            </tr>
          </tbody>
        </table>
    
        <div class="table-area-selected"></div>
      </div>
    </body>
    
    </html>

    演示:http://plnkr.co/edit/RIhDiu9bI00SJysKvMuu?p=preview

    【讨论】:

    • 我最初使用 HTML Drag & Drop 尝试过这个,但找不到任何方法让它做你想要的。
    【解决方案2】:

    只是一个更简单的示例,但您可以使用多个选择框。 Colspan 可以与 .data-cell 大小相关。我还添加了user-select: none; css 属性以避免在拖动时随机选择文本。

    $(function() {
      var dragging = false;
      var selected = null;
      var cell = $('table').find('.data-cell');
    
      $('.table-area-selected').each(function(index) {
        $(this).css('top', $(cell.get(index)).position().top);
        $(this).css('left', $(cell.get(index)).position().left);
    
        $(this).css('width', $(cell.get(index)).innerWidth());
        $(this).css('height', $(cell.get(index)).innerHeight());
      });
    
      $('.table-area-selected').on('mousedown', function(event) {
        console.log('mousedown');
        selected = $(this);
        dragging = true;
    
      });
    
      $(window).on('mouseup', function(event) {
        console.log('mouseup');
        selected = null;
        dragging = false;
      });
    
      $('.data-cell').on('mouseover', function(event) {
        var cell = $(this);
        if (dragging) {
          selected.css('top', cell.position().top);
          selected.css('left', cell.position().left);
    
          selected.css('width', cell.innerWidth());
          selected.css('height', cell.innerHeight());
          console.log('dragged a selection box : ' + selected);
        }
      });
    
    });
    table th,
    table td {
      padding: 10px 50px;
      border: 1px solid #cecece;
    }
    
    .table-area {
      position: relative;
      -webkit-user-select: none;
      /* Chrome, Opera, Safari */
      -moz-user-select: none;
      /* Firefox 2+ */
      -ms-user-select: none;
      /* IE 10+ */
      user-select: none;
      /* Standard syntax */
    }
    
    .table-area-selected {
      position: absolute;
      background: green;
      border: 1px solid blue;
      cursor: pointer;
    }
    
    .data-cell {}
    <!DOCTYPE html>
    <html>
    
    <head>
      <script data-require="jquery@*" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <link rel="stylesheet" href="style.css" />
      <script src="script.js"></script>
    </head>
    
    <body>
      <h1>Hello Plunker!</h1>
    
      <div class="table-area">
        <table>
          <thead>
            <tr>
              <th>someDate</th>
              <th>1</th>
              <th>2</th>
              <th>3</th>
            </tr>
          </thead>
    
          <tbody>
            <tr>
              <td>someDateVal1</td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
            </tr>
            <tr>
              <td>someDateVal2</td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
              <td class="data-cell"></td>
            </tr>
          </tbody>
        </table>
    
        <div class="table-area-selected"></div>
        <div class="table-area-selected"></div>
      </div>
    </body>
    
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2019-10-18
      • 2015-10-24
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多