<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();
});
}
} );
});
This bin was created anonymously and its free preview time has expired. Get a free unrestricted account
Dismiss xShortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + [ | Indents selected lines |
ctrl + ] | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Clone Bin |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |