Created
January 18, 2017 08:10
-
-
Save mass6/41fa98d7508cc54afcd8b0772d083315 to your computer and use it in GitHub Desktop.
JQuery-UI Sortable table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>jQuery UI Sortable - Default functionality</title> | |
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> | |
<link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> | |
<style> | |
body { | |
font-family: Arial, Helvetica, sans-serif; | |
} | |
table { | |
font-size: 1em; | |
} | |
.ui-draggable, .ui-droppable { | |
background-position: top; | |
} | |
.ui-state-highlight { height: 1.5em; line-height: 1.2em; } | |
/*#sortable li span.ordernum { | |
display: inline-block; | |
margin-left: 50px; | |
position: relative; | |
} | |
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; } | |
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em;} | |
#sortable li span { position: absolute; margin-left: -1.3em; }*/ | |
</style> | |
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> | |
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<section class="panel panel-default"> | |
<header class="panel-heading clearfix"> | |
<h3 class="panel-title">List Name <small>(3)</small></h3> | |
</header> | |
<div class="panel-body"> | |
<table class="table table-bordered table-striped"> | |
<thead> | |
<th>Name</th> | |
<th>Position</th> | |
</thead> | |
<tbody> | |
<tr id="1"> | |
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> LA Shop</td> | |
<td class="ordernum">position: 1</td> | |
</tr> | |
<tr id="2"> | |
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> NYC Tatttoo</td> | |
<td class="ordernum">position: 2</td> | |
</tr> | |
<tr id="3"> | |
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> CPH Shop</td> | |
<td class="ordernum">position: 3</td> | |
</tr> | |
<tr id="4"> | |
<td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> Miami Tattoo Shop</td> | |
<td class="ordernum">position: 4</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
</section> | |
<!-- <ul id="sortable"> | |
<li id="1" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> LA Shop <span id="order_1" class="ordernum pull-right">position: 1</span></li> | |
<li id="2" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> NYC Tatttoo <span id="order_2" class="ordernum pull-right">position: 2</span></li> | |
<li id="3" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> CPH Shop <span id="order_3" class="ordernum pull-right">position: 3</span></li> | |
<li id="4" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> Miami Tattoo Shop <span id="order_4" class="ordernum pull-right">position: 4</span></li> | |
</ul> --> | |
<br/> | |
<div> | |
<button class="btn btn-success">Save Order</button> | |
</div> | |
</div> | |
</body> | |
<script> | |
$( function() { | |
$( "tbody" ).sortable({ | |
placeholder: "ui-state-highlight" | |
}); | |
$( "tbody" ).disableSelection(); | |
}); | |
var sortableList = $("tbody"); | |
var order; | |
var sortEventHandler = function(event, ui){ | |
console.log("New sort order!"); | |
order = sortableList.sortable( "toArray" ); | |
console.log(order); | |
$.each(sortableList.children(), function(i, el) { | |
$(el).children('.ordernum').text("position: " + (i+1)); | |
}); | |
}; | |
sortableList.on("sortstop", sortEventHandler); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment