TableDnD | jQuery plug-in to drag and drop rows in HTML tables
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../../../js/jquery-3.5.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/TableDnD/0.9.1/jquery.tablednd.js" integrity="sha256-d3rtug+Hg1GZPB7Y/yTcRixO/wlI78+2m08tosoRn7A=" crossorigin="anonymous"></script>
</head>
<body>
<table id="table-1" cellspacing="0" cellpadding="2">
<tr id="1"><td>1</td><td>One</td><td>1 text</td></tr>
<tr id="2"><td>2</td><td>Two</td><td>2 text</td></tr>
<tr id="3"><td>3</td><td>Three</td><td>3 text</td></tr>
<tr id="4"><td>4</td><td>Four</td><td>4 text</td></tr>
<tr id="5"><td>5</td><td>Five</td><td>5 text</td></tr>
<tr id="6"><td>6</td><td>Six</td><td>6 text</td></tr>
</table>
<div id="debugArea"></div>
<script type="text/javascript">
$(document).ready(function() {
// Initialise the table
$("#table-1").tableDnD({
onDragClass: "myDragClass",
onDrop: function(table, row) {
var rows = table.tBodies[0].rows;
var debugStr = "Row dropped was "+row.id+". New order: ";
for (var i=0; i<rows.length; i++) {
debugStr += rows[i].id+" ";
}
$("#debugArea").html(debugStr);
},
onDragStart: function(table, row) {
$("#debugArea").html("Started dragging row "+row.id);
}
})
});
$("#table-1 tr:even").addClass('alt')
$("#table-1").onDragStop=function (table,row) {
console.log(table)
console.log(row)
}
</script>
</body>
</html>
|