<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
</head>
<style>
.ghost {
opacity: 0.4;
}
.list-group {
margin: 20px;
}
button {
margin: 40px 20px;
float: right;
}
</style>
<body>
<h4 style="margin: 20px 0">SortableJS Sandbox with jQuery SortableJS</h4>
<p>
This sandbox uses the <a href="https://github.com/SortableJS/sortablejs">SortableJS</a> library
along with the <a href="https://github.com/SortableJS/jquery-sortablejs">jQuery SortableJS</a> binding.
Features illustrated here are sorting, groups, the onSort event, and collecting the sorted elements in an array.
</p>
Drag and drop items in each list, and drag them between lists (groups). Then click the button
to read the order in the console.
</p>
<p>
Observe that the 'toArray' function collects the 'data-id' for each element.
<p>
The necessary libraries are in the script tags below. In my WordPress testing environment
jQuery 2.2.4 is required. Even though WordPress bundles jQuery, the version supplied doesn't
seem to support the animation effects in SortableJS -- it's just very rough without jQuery
2.2.4.
</p>
<div id="demo" class="row">
<div id="items-1" class="list-group col">
<div id="item-1.1" data-id="1.1" class="list-group-item nested-1">Item 1.1</div>
<div id="item-1.2" data-id="1.2" class="list-group-item nested-1">Item 1.2</div>
<div id="item-1.3" data-id="1.3" class="list-group-item nested-1">Item 1.3</div>
<div id="item-1.4" data-id="1.4" class="list-group-item nested-1">Item 1.4</div>
<div id="item-1.5" data-id="1.5" class="list-group-item nested-1">Item 1.5</div>
</div>
<div id="items-2" class="list-group col">
<div id="item-2.1" data-id="2.1" class="list-group-item nested-1">Item 2.1</div>
<div id="item-2.2" data-id="2.2" class="list-group-item nested-1">Item 2.2</div>
<div id="item-2.3" data-id="2.3" class="list-group-item nested-1">Item 2.3</div>
<div id="item-2.4" data-id="2.4" class="list-group-item nested-1">Item 2.4</div>
<div id="item-2.5" data-id="2.5" class="list-group-item nested-1">Item 2.5</div>
</div>
</div>
<button id="get-order" >Get Order</button>
<script src="https://unpkg.com/sortablejs-make/Sortable.min.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-sortablejs@latest/jquery-sortable.js"></script>
</body>
</html>
// List 1
$('#items-1').sortable({
group: 'list',
animation: 200,
ghostClass: 'ghost',
onSort: reportActivity,
});
// List 2
$('#items-2').sortable({
group: 'list',
animation: 200,
ghostClass: 'ghost',
onSort: reportActivity,
});
// Arrays of "data-id"
$('#get-order').click(function() {
var sort1 = $('#items-1').sortable('toArray');
console.log(sort1);
var sort2 = $('#items-2').sortable('toArray');
console.log(sort2);
});
// Report when the sort order has changed
function reportActivity() {
console.log('The sort order has changed');
};
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | 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 + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
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. |