$('tbody#sortThis').sortable({
start: function (event, ui) {
//Let's set the starting position of an row in the table to ensure the correct row index
var start_pos = ui.item.index() + 1;
ui.item.data('start_pos', start_pos);
},
change: function (event, ui) {
//When the item postion is changed while dragging a item, get it's position
var start_pos = ui.item.data('start_pos');
//get the direction of the drag motion
var dragDirection = start_pos < ui.placeholder.index() ? "down" : "up";
ui.item.data('drag_direction', dragDirection);
},
stop: function (event, ui) {
//get the table where the sorting is happening
var thisTable = document.getElementById('formTable');
//get the row index of where the dragged row has stopped
var movedRowIndex = (ui.item.index() + 1);
//get the page # cell and update the text to the stopped location
var pageNumberCell = $(thisTable.rows[movedRowIndex].cells[1]);
pageNumberCell.text((ui.item.index() + 1));
//get the direction 'up' or 'down' that the record was dragged.
var drag_direction = ui.item.data('drag_direction');
//if the record was drageed down the table, update record page#s above
if (drag_direction == 'down') {
for (var i = 1; i < thisTable.rows.length; i++) {
if (i < movedRowIndex) {
$(thisTable.rows[i].cells[1]).text(i);
}
}
}
//if the record was drageed up the table, update record page#s below
if (drag_direction == 'up') {
for (var i = 1; i < thisTable.rows.length; i++) {
if (i > movedRowIndex) {
$(thisTable.rows[i].cells[1]).text(i);
}
}
}
}
});
$('tbody#sortThis').disableSelection();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.