Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.18/datatables.min.css"/>
 
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.18/datatables.min.js"></script>
</head>
<body>
    <div class="container">
      <table id="example" class="display nowrap" width="100%">
        <thead>
          <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
          </tr>
        </thead>
        <tfoot>
          <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
          </tr>
        </tfoot>
        <tbody>
          <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$3,120</td>
          </tr>
          <tr>
            <td>Garrett Winters</td>
            <td>Director</td>
            <td>Edinburgh</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>$5,300</td>
          </tr>
          <tr>
            <td>Ashton Cox</td>
            <td>Technical Author</td>
            <td>San Francisco</td>
            <td>66</td>
            <td>2009/01/12</td>
            <td>$4,800</td>
          </tr>
          <tr>
            <td>Cedric Kelly</td>
            <td>Javascript Developer</td>
            <td>Edinburgh</td>
            <td>22</td>
            <td>2012/03/29</td>
            <td>$3,600</td>
          </tr>
          <tr>
            <td>Jenna Elliott</td>
            <td>Financial Controller</td>
            <td>Edinburgh</td>
            <td>33</td>
            <td>2008/11/28</td>
            <td>$5,300</td>
          </tr>
          <tr>
            <td>Brielle Williamson</td>
            <td>Integration Specialist</td>
            <td>New York</td>
            <td>61</td>
            <td>2012/12/02</td>
            <td>$4,525</td>
          </tr>
        </tbody>
      </table>
    </div>
  
  
</body>
</html>
 
$(document).ready( function () {
  var dragSrc = null;  //Globally track source cell
  var cells = null;  // All cells in table
  
  var table = $('#example').DataTable({
    pageLength: 3,
    columnDefs: [ {
      targets: '_all',
      
      // Set HTML5 draggable for all cells
      createdCell: function (td, cellData, rowData, row, col) {
        $(td).attr('draggable', 'true');
      }
    } ],
    drawCallback: function () {
      // Apply HTML5 drag and drop listeners to all cells
      cells = document.querySelectorAll('#example td');
        [].forEach.call(cells, function(cell) {
          cell.addEventListener('dragstart', handleDragStart, false);
          cell.addEventListener('dragenter', handleDragEnter, false)
          cell.addEventListener('dragover', handleDragOver, false);
          cell.addEventListener('dragleave', handleDragLeave, false);
          cell.addEventListener('drop', handleDrop, false);
          cell.addEventListener('dragend', handleDragEnd, false);
        });
    }
  });
  
  
function handleDragStart(e) {
  this.style.opacity = '0.4';  // this / e.target is the source node.
  dragSrc = this;  // Keep track of source cell
  // Allow moves
  e.dataTransfer.effectAllowed = 'move';
  
  // Get the cell data and store in the transfer data object
  e.dataTransfer.setData('text/html', this.innerHTML);
}
function handleDragOver(e) {
  if (e.preventDefault) {
    e.preventDefault(); // Necessary. Allows us to drop.
  }
  // Allow moves
  e.dataTransfer.dropEffect = 'move';  // See the section on the DataTransfer object.
  return false;
}
function handleDragEnter(e) {
  // this / e.target is the current hover target.
  
  // Apply drop zone visual
  this.classList.add('over');
}
function handleDragLeave(e) {
  // this / e.target is previous target element.
  
  // Remove drop zone visual
  this.classList.remove('over');  
}
function handleDrop(e) {
  // this / e.target is current target element.
  if (e.stopPropagation) {
    e.stopPropagation(); // stops the browser from redirecting.
  }
  // Don't do anything if dropping the same column we're dragging.
  if (dragSrc != this) {
    // Set the source column's HTML to the HTML of the column we dropped on.
    dragSrc.innerHTML = this.innerHTML;
    
    // Set the distination cell to the transfer data from the source
    this.innerHTML = e.dataTransfer.getData('text/html');
    // Invalidate the src cell and dst cell to have DT update its cache then draw
    table.cell(dragSrc).invalidate();
    table.cell(this).invalidate().draw(false);
  }
  return false;
}
function handleDragEnd(e) {
  // this/e.target is the source node.
  this.style.opacity = '1.0';
  [].forEach.call(cells, function (cell) {
    // Make sure to remove drop zone visual class
    cell.classList.remove('over');
  });
}
} );
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
kthorngrenpro
0viewers