I have a <ul>
list that I want to make sortable (drag & drop). How can I get it to work with Bootstrap 3 in modern browsers and touch devices?
I'm trying to use jqueryui-sortable combined with http://touchpunch.furf.com/; it seems to work, but it's hacky and jQueryUI doesn't play nice with Bootstrap.
How can I avoid Bootstrap/jQueryUI conflicts while adding touch-screen support?
B1 – Set <li draggable>. B2 – On drag start, attach .hint to highlight all the possible drop zones. B3 – When the dragged element hovers a dropzone, attach .active to show a different highlight color. B4 – When the dragged element leaves a dropzone, detach .active. B5 – When the drag stops, remove all .hint and .active CSS classes. B6 – Necessary…
Touch-dnd is a powerful jQuery plugin which provides smooth, touch-enabled drag'n'drop and sortable functionalities on a list or grid using CSS3 transforms and transitions. A great and advanced alternative to the jQuery UI draggable, sortable, and dropppable widgets.
The drag handle is the area of the draggable element that you click or touch to pick up and move an item. It also can be called the draggable area: The example above shows small handle, which can work for somebody on a desktop device with a mouse cursor, but may be tricky for a chubby finger on a small touch screen.
B – Define the <ul> or <ol> as usual. Take note that this simple example will only work with a flat one-level list. C – Use the slist (TARGET) function on window load to initiate the sortable list. These are just some simple cosmetic styles for the sortable list. A – The Javascript will attach a .slist class on the container.
The answers posted before this one are surprisingly outdated.
rubaxa-sortable is a fast, no-dependencies, small reorderable lists widget with touch support that works with the HTML5 native drag&drop API. You can use it with Bootstrap, Foundation, or any CSS library you want, and instantiating it only takes one line.
It supports reordering within a list or across lists. You can define lists that can only receive elements, or lists from which you can drag, but onto which you cannot drop. It's also very actively maintained and MIT licensed on GitHub.
new Sortable(document.getElementsByClassName('sortable')[0]);
<!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> <!-- Sortable --> <script src="https://rawgit.com/RubaXa/Sortable/master/Sortable.js"></script> <ul class="list-group sortable"> <li class="list-group-item">This is <a href="http://rubaxa.github.io/Sortable/">Sortable</a></li> <li class="list-group-item">It works with Bootstrap...</li> <li class="list-group-item">...out of the box.</li> <li class="list-group-item">It has support for touch devices.</li> <li class="list-group-item">Just drag some elements around.</li> </ul>
Just thought I'd post a supplemental answer to the one from @KyorCode. I followed his advice and went with JQuery Sortable and it worked seamlessly for me. It took me maybe 10 minutes to get this working.
I didn't have to include any JQuery UI CSS - just the JavaScript - so there were no issues with the CSS conflicting in anyway with Bootstrap.
Working Example:
Here's a working example on www.bootply.com.
HTML:
<!-- Bootstrap 3 panel list. --> <ul id="draggablePanelList" class="list-unstyled"> <li class="panel panel-info"> <div class="panel-heading">You can drag this panel.</div> <div class="panel-body">Content ...</div> </li> <li class="panel panel-info"> <div class="panel-heading">You can drag this panel too.</div> <div class="panel-body">Content ...</div> </li> </ul>
JavaScript:
You can download JQuery Sortable without including the whole of JQuery UI in your page.
<!-- Assumes that JQuery is already included somewhere. Size: 22kb or 13kb minified. --> <script src="/Scripts/jquery-ui-1.10.4.custom.js"></script> <script type="text/javascript"> jQuery(function($) { var panelList = $('#draggablePanelList'); panelList.sortable({ // Only make the .panel-heading child elements support dragging. // Omit this to make the entire <li>...</li> draggable. handle: '.panel-heading', update: function() { $('.panel', panelList).each(function(index, elem) { var $listItem = $(elem), newIndex = $listItem.index(); // Persist the new indices. }); } }); }); </script>
CSS:
<style type="text/css"> /* show the move cursor as the user moves the mouse over the panel header.*/ #draggablePanelList .panel-heading { cursor: move; } </style>
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With