JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://cdn.datatables.net/1.10.3/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="http://cdn.datatables.net/1.10.3/css/jquery.dataTables.css">
<div class="toolbar-buttons">
<button type="button" class="up">UP</button>
<button type="button" class="down">DOWN</button>
</div>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr id="1">
<td>1</td>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr id="2">
<td>2</td>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr id="3">
<td>3</td>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr id="4">
<td>4</td>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
...
CSS
.toolbar-buttons{
margin-bottom: 32px;
}
JavaScript
$(document).ready(function() {
var oTable = $('#example').DataTable();
/* select row and enable edit & delete buttons */
$('#example').delegate('tr', 'click', function (e) {
$(this).addClass('selected').siblings().removeClass('selected');
});
/* get row id */
$('#example').on('click','td',function (e) {
var id = $(this).closest('tr').attr('id');
window.id = id;
});
$(document).on('click', '.up', function(e){
moveSelected("up");
});
$(document).on('click', '.down', function(e){
moveSelected("down");
});
function moveSelected(direction){
var arr = jQuery('#example tbody tr.selected');
for(var i=0; i<arr.length; i++) {
var tr = arr[i];
var row = jQuery(tr); // row to move.
var prevRow;
if(direction === "up")
prevRow = jQuery(tr).prev();
else
prevRow = jQuery(tr).next();
/* already at the top? */
if(prevRow.length==0){ break; }
moveDataUp(row, prevRow);
moveVisualSelectionUp(row, prevRow);
}
}
/* the visual stuff that show which rows are selected */
function moveVisualSelectionUp(row, prevRow){
row.removeClass("selected");
prevRow.addClass("selected");
}
/* move the data in the internal datatable structure */
function moveDataUp(row, prevRow){
var oTable = $('#example').DataTable();
var movedData = oTable.fnGetData(row[0]).slice(0); // copy of row to move.
var prevData = oTable.fnGetData(prevRow[0]).slice(0); // copy of old data to be overwritten by above data.
// switch data around :)
oTable.fnUpdate(prevData , row[0], 0, false, false);
oTable.fnUpdate(movedData , prevRow[0], 0, true, true);
}
});