nestedSortable jQuery Plugin

Sort and move list items between nested lists

Latest version: 1.3.4 / May 11, 2011

Download the latest version directly from the GitHub page.

For QA, feature requests and general support please visit the nestedSortable forum.

  1. Item 1
  2. Item 2
    1. Sub Item 2.1
    2. Sub Item 2.2
    3. Sub Item 2.3
    4. Sub Item 2.4
  3. Item 3 (no-nesting)
  4. Item 4 (no-nesting)
  5. Item 5
  6. Item 6
    1. Sub Item 6.1
    2. Sub Item 6.2 (no-nesting)
    3. Sub Item 6.3
    4. Sub Item 6.4
  7. Item 7
  8. Item 8

Try the custom methods:




	



	


	

Note: This demo has the maxLevels option set to '3'.

Features:

Usage:

<ol class="sortable">
	<li><div>Some content</div></li>
	<li><div>Some content</div>
		<ol>
			<li><div>Some sub-item content</div></li>
			<li><div>Some sub-item content</div></li>
		</ol>
	</li>
	<li><div>Some content</div></li>
</ol>

<div> can be any tag (e.g. <span>), but it has to be some tag. This same tag has to be set as the 'toleranceElement' in the options, and this, or one of its children, as the 'handle'.
Any <li> can have either one or two tags, the second being <ol>.

Custom options:

tabSize
How far right or left (in pixels) the item has to travel in order to be nested or to be sent outside its current list. Default: 20
disableNesting
The class name of the items that will not accept nested lists. Default: 'ui-nestedSortable-no-nesting'
errorClass
The class given to the placeholder in case of error. Default: 'ui-nestedSortable-error'
listType
The list type used (ordered or unordered). Default: 'ol' (see 1.2 Changelog)
maxLevels
How many nesting levels can the list have at the most. If set to '0' the levels are unlimited. Default: '0'
noJumpFix
Set this to '1' to deactivate the fix (see 1.2.2 Changelog), in case it messes things up for you. Default: '0'

Custom methods:

serialize
Serializes the nested list into a string like setName[itemId]=parentId&setName[itemId]=parentId from each item's id reading 'setName_itemId' (where itemId is a number).
It accepts the same options as the UI Sortable method (key, attribute and expression).
toArray
Builds an array where each element is in the form:

setName[n] =>
{
	'item_id': itemId,
	'parent_id': parentId,
	'depth': depth,
	'left': left,
	'right': right,
}
looking at the items' id's as in serialize.
The left and right values comply with the nested set model.
This method accepts two options:
startDepthCount sets the starting depth number (default is 0), expression is the regex used to extract the number from the item's id (default is the same as serialize), attribute is the element's attribute that the method will look at (default is 'id').
The first array element is the root element which contains all the others, and it is built from the available data with no need for any special entry.
toHierarchy
Builds a hierarchical object with data from the nested elements in the form:

'0' ...
	'id' => itemId
'1' ...
	'id' => itemId
	'children' ...
		'0' ...
			'id' => itemId
		'1' ...
			'id' => itemId
'2' ...
	'id' => itemId
Similarly to toArray, it accepts the attribute and expression options.

1.3.4 Changelog:

1.3.3 Changelog:

1.3.2 Changelog:

1.3.1 Changelog:

1.3 Changelog:

1.2.3 Changelog:

1.2.2 Changelog:

1.2.1 Changelog:

1.2b Changelog:

1.2 Changelog:

1.1.1 Changelog:

1.1 Changelog:

1.0.3 Changelog:

1.0.2 Changelog:

Known bugs:

Text gets ugly when you sort items in some versions of IE, in certain situation. This is a sortable bug.

Roadmap:

1.1
Adapt serialize and toArray in order to have it show the indentation level, somehow.
1.3
Make a maxLevel option.

Requires:

jQuery 1.4+
jQuery UI Sortable 1.8+

Browser compatibility:

Tested with: IE 6, Firefox 3.6, Chrome 10, Safari 3
Will also work with: IE 6+, Firefox 3.5+, Chrome 4+, Safari 3+

Licence:

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

This work is now pizzaware. If it saved your life, or you just feel good at heart, please consider offering me a pizza. This can be done in two ways: (1) use the Paypal button below; (2) send me cash via traditional mail to my home address in Italy. Is the second method legal? It is in Italy if you use Posta assicurata. You should check with your local laws if you live elsewhere.

contact me

© 2010–2012 Manuele J Sarfatti