Sortable & Draggable Item List with jQuery and jQuery UI

File Size: 4.16 KB
Views Total: 30003
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Sortable & Draggable Item List with jQuery and jQuery UI

A lightweight jQuery plugin which allows you to sort a nested item list with jQuery UI based drag & drop.

How to use it:

1. Include the jQuery javascript library and jQuery UI in the document.

<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script type='text/javascript' src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> 

2. Create a nested list using Html unordered list.

<div class="container">
  <h3 class="title" id="title0">jQuery Nested Sortable Plugin Demo</h3>
  <ul class="space first-space" id="space0">
    <li class="route">
      <h3 class="title" id="title1">A</h3>
      <span class="ui-icon ui-icon-arrow-4-diag"></span>
      <ul class="space" id="space1">
      </ul>
    </li>
    <li class="route">
      <h3 class="title" id="title2">B</h3>
      <span class="ui-icon ui-icon-arrow-4-diag"></span>
      <ul class="space" id="space2">
        <li class="route">
          <h3 class="title" id="title3">C</h3>
          <span class="ui-icon ui-icon-arrow-4-diag"></span>
          <ul class="space" id="space3">
          </ul>
        </li>
      </ul>
    </li>
    <li class="route">
      <h3 class="title" id="title4">D</h3>
      <span class="ui-icon ui-icon-arrow-4-diag"></span>
      <ul class="space" id="space4">
      </ul>
    </li>
    <li class="route">
      <h3 class="title">E</h3>
      <span class="ui-icon ui-icon-arrow-4-diag"></span>
      <ul class="space">
      </ul>
    </li>
    <li class="route">
      <h3 class="title">F</h3>
      <span class="ui-icon ui-icon-arrow-4-diag"></span>
      <ul class="space">
      </ul>
    </li>
    <li class="route">
      <h3 class="title">G</h3>
      <span class="ui-icon ui-icon-arrow-4-diag"></span>
      <ul class="space">
      </ul>
    </li>
    <li class="route">
      <h3 class="title">H</h3>
      <span class="ui-icon ui-icon-arrow-4-diag"></span>
      <ul class="space">
      </ul>
    </li>
  </ul>
</div>

3. The sample CSS to style the list.

.container {
  position: relative;
  margin-top: 60px;
  margin-left: 60px;
  margin-right: 60px;
  padding-bottom: 10px;
  min-height: 500px;
  background: #eee;
  box-shadow: 0px 0px 10px 2px #bbb;
}

.container h3 {
  position: absolute;
  border: 0;
  margin: 0;
  padding: 0;
  padding-top: 14px;
  height: 44px;
  width: 400px;
  text-indent: 80px;
  background: #4af;
  border-radius: 2px;
  box-shadow: 0px 0px 0px 2px #29f;
  pointer-events: none;
  margin-left: 0px;
  width: 100%;
  background: white;
  box-shadow: 0px 2px 0px 1px #9bf;
}

.route {
  position: relative;
  list-style-type: none;
  border: 0;
  margin: 0;
  padding: 0;
  top: 0px;
  margin-top: 0px;
  max-height: 100% !important;
  width: 100%;
  background: #bcf;
  border-radius: 2px;
  z-index: -1;
}

.route span {
  position: absolute;
  top: 20px;
  left: 20px;
  -ms-transform: scale(2);
  /* IE 9 */

  -webkit-transform: scale(2);
  /* Chrome, Safari, Opera */

  transform: scale(2);
  z-index: 10px;
}

.route .title {
  position: absolute;
  border: 0;
  margin: 0;
  padding: 0;
  padding-top: 14px;
  height: 44px;
  width: 400px;
  text-indent: 80px;
  background: #4af;
  border-radius: 2px;
  box-shadow: 0px 0px 0px 2px #29f;
  pointer-events: none;
}

.first-title { margin-left: 10px; }

.space {
  position: relative;
  list-style-type: none;
  border: 0;
  margin: 0;
  padding: 0;
  margin-left: 70px;
  width: 60px;
  top: 68px;
  padding-bottom: 68px;
  height: 100%;
  z-index: 1;
}

.first-space { margin-left: 10px; }

4. The Javascript to enable the sortable list.

$(document).ready(function(){

calcWidth($('#title0'));

window.onresize = function(event) {
    console.log("window resized");

    //method to execute one time after a timer

    };

//recursively calculate the Width all titles
function calcWidth(obj){
console.log('---- calcWidth -----');

var titles = 
$(obj).siblings('.space').children('.route').children('.title');

$(titles).each(function(index, element){
var pTitleWidth = parseInt($(obj).css('width'));
var leftOffset = parseInt($(obj).siblings('.space').css('margin-left'));

var newWidth = pTitleWidth - leftOffset;

if ($(obj).attr('id') == 'title0'){
console.log("called");

newWidth = newWidth - 10;
}

$(element).css({
'width': newWidth,
})

calcWidth(element);
});

}

$('.space').sortable({
connectWith:'.space',
// handle:'.title',
// placeholder: ....,
tolerance:'intersect',
over:function(event,ui){
},
receive:function(event, ui){
calcWidth($(this).siblings('.title'));
},
});

$('.space').disableSelection();

});

This awesome jQuery plugin is developed by BDG310. For more Advanced Usages, please check the demo page or visit the official website.