<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
    <link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
    <link href="https://nightly.datatables.net/rowreorder/css/rowReorder.dataTables.css?_=9e78fb9f95a9b67ea132905fd3877eea.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/rowreorder/js/dataTables.rowReorder.js?_=9e78fb9f95a9b67ea132905fd3877eea"></script>
    <script src="https://cdn.datatables.net/plug-ins/1.10.20/sorting/absolute.js"></script>
    <meta charset=utf-8 />
    <title>DataTables - JS Bin</title>
  </head>
  <body>
    <div class="container">
      <table id="roles_table" class="display nowrap" width="100%">
        <thead>
          <tr>
            <th>Rank</th>
            <th>Role</th>
            <th>datas</th>
            <th>datas</th>
        </tr>
        </thead>
        <tfoot>
          <tr>
            <th>Rank</th>
            <th>Role</th>
            <th>datas</th>
            <th>datas</th>
        </tr>
        </tfoot>
        <tbody>
          <tr>
            <td>1</td>
            <td>owner</td>
            <td>some data</td>
            <td>some data</td>
          </tr>
          <tr>
            <td>2</td>
            <td>admin</td>
            <td>some data</td>
            <td>some data</td>
          </tr>
          <tr>
            <td>3</td>
            <td>mod</td>
            <td>some data</td>
            <td>some data</td>
          </tr>
          <tr>
            <td>4</td>
            <td>user</td>
            <td>some data</td>
            <td>some data</td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>
 
body {
  font: 90%/1.45em "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
  margin: 0;
  padding: 0;
  color: #333;
  background-color: #fff;
}
table.dataTable td.reorder {
  text-align: unset !important;
}
 
$(document).ready( function () {
var topIndex = 1;
var reorderType = $.fn.dataTable.absoluteOrderNumber([
    { value: topIndex, position: 'top' }
]);
var rolesTable = $('#roles_table').DataTable({
    rowReorder: { selector: '.allow-reorder' },
    columnDefs: [
        { targets: '_all', orderable: false },
        { targets: [2, 3], width: '10%', searchable: false },
        {
            targets: 0,
            type: reorderType,
            className: 'allow-reorder reorder',
            createdCell: function (td, cellData, rowData, row, col) {
                /*
                console.log('createdCell');
                if (!swap) {
                    console.log('push state');
                    state.push(rowData.slice()); // push a copy of rowData array
                }
                */
              
                if (cellData == topIndex) {
                    $(td).removeClass('allow-reorder reorder');
                }
            }
        }
    ]
});
  rolesTable.on( 'row-reordered', function ( e, diff, edit ) {
    console.log(diff);
  
    var topRow = false;  // Is top row involved in reorder?
    
    // Look for top row being reordered
    for (i=0; i<diff.length; i++) {
      d = diff[i];
      
      // Set topRow flag to 
      if (d.oldData == 1) {
        topRow = true;
        break;
      }
    }
  
    if (topRow) {
      // Fix the order after table draws
      rolesTable
        .one( 'draw', function () {
          
          // Itereate all reordered rows to reset them back to "oldData"
          for (i=0; i<diff.length; i++) {
            
            // Get the Datatable row
            var row = rolesTable.row( diff[i].node );
            
            // Get the row data
            var data = row.data();
            
            // Update the index column with the oldData (old index)
            data[0] = diff[i].oldData;
            
            // Apply the updated data
            row.data(data);
          }
        
          // Sort the table after the updates
          rolesTable.draw();
            
        });
    }
  } );
  
  
});
Output 300px

This bin was created anonymously and its free preview time has expired. Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers