I cannot provide any support or guidance beyond this README. If this code helps you that's great but I have no plans to develop Nestable beyond this demo (it's not a final product and has limited functionality). I cannot reply to any requests for help..
I'm picking up active developement for Nestable! Pull requests are welcome!
Nestable is an experimental example and IS under active development. If it suits your requirements feel free to expand upon it!
Write your nested HTML lists like so:
<div class="dd">
<ol class="dd-list">
<li class="dd-item" data-id="1">
<div class="dd-handle">Item 1</div>
</li>
<li class="dd-item" data-id="2">
<div class="dd-handle">Item 2</div>
</li>
<li class="dd-item" data-id="3">
<div class="dd-handle">Item 3</div>
<ol class="dd-list">
<li class="dd-item" data-id="4">
<div class="dd-handle">Item 4</div>
</li>
<li class="dd-item" data-id="5" data-foo="bar">
<div class="dd-handle">Item 5</div>
</li>
</ol>
</li>
</ol>
</div>Then activate with jQuery like so:
$('.dd').nestable({ /* config options */ });change: For using an .on handler in jquery
The callback provided as an option is fired when elements are reordered or nested.
$('.dd').nestable({
callback: function(l,e){
// l is the main container
// e is the element that was moved
}
});You can get a serialised object with all data-* attributes for each item.
$('.dd').nestable('serialize');The serialised JSON for the example above would be:
[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5,"foo":"bar"}]}]You can passed serialized JSON as an option if you like to dynamically generate a Nestable list:
<div class="dd" id="nestable-json"></div>
<script>
var json = '[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5,"foo":"bar"}]}]';
var options = {'json': json }
$('#nestable-json').nestable(options);
</script>NOTE: serialized JSON has been expanded so that an optional "content" property can be passed which allows for arbitrary custom content (including HTML) to be placed in the Nestable item
Or do it yourself the old-fashioned way:
<div class="dd" id="nestable3">
<ol class='dd-list dd3-list'>
<div id="dd-empty-placeholder"></div>
</ol>
</div>
<script>
$(document).ready(function(){
var obj = '[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}]';
var output = '';
function buildItem(item) {
var html = "<li class='dd-item' data-id='" + item.id + "'>";
html += "<div class='dd-handle'>" + item.id + "</div>";
if (item.children) {
html += "<ol class='dd-list'>";
$.each(item.children, function (index, sub) {
html += buildItem(sub);
});
html += "</ol>";
}
html += "</li>";
return html;
}
$.each(JSON.parse(obj), function (index, item) {
output += buildItem(item);
});
$('#dd-empty-placeholder').html(output);
$('#nestable3').nestable();
});
</script>You can change the follow options:
maxDepthnumber of levels an item can be nested (default5)groupgroup ID to allow dragging between lists (default0)callbackcallback function when an element has been changed (defaultnull)
These advanced config options are also available:
contentCallbackThe callback for customizing content (defaultfunction(item) {return item.content || '' ? item.content : item.id;})listNodeNameThe HTML element to create for lists (default'ol')itemNodeNameThe HTML element to create for list items (default'li')rootClassThe class of the root element.nestable()was used on (default'dd')listClassThe class of all list elements (default'dd-list')itemClassThe class of all list item elements (default'dd-item')dragClassThe class applied to the list element that is being dragged (default'dd-dragel')noDragClassThe class applied to an element to prevent dragging (default'dd-nodrag')handleClassThe class of the content element inside each list item (default'dd-handle')collapsedClassThe class applied to lists that have been collapsed (default'dd-collapsed')noChildrenClassThe class applied to items that cannot have children (default'dd-nochildren')placeClassThe class of the placeholder element (default'dd-placeholder')emptyClassThe class used for empty list placeholder elements (default'dd-empty')expandBtnHTMLThe HTML text used to generate a list item expand button (default'<button data-action="expand">Expand></button>')collapseBtnHTMLThe HTML text used to generate a list item collapse button (default'<button data-action="collapse">Collapse</button>')includeContentEnable or disable the content in output (defaultfalse)listRendererThe callback for customizing final list output (defaultfunction(children, options) { ... }- see defaults in code)itemRendererThe callback for customizing final item output (defaultfunction(item_attrs, content, children, options) { ... }- see defaults in code)jsonJSON string used to dynamically generate a Nestable list. This is the same format as theserialize()output
Inspect the Nestable Demo for guidance.
- [zemistr] Created listRenderer and itemRenderer. Refactored build from JSON.
- [zemistr] Added support for adding classes via input data. (
[{"id": 1, "content": "First item", "classes": ["dd-nochildren", "dd-nodrag", ...] }, ... ])
- [zemistr] Added support for additional data parameters.
- [zemistr] Added callback for customizing content.
- [zemistr] Added parameter "includeContent" for including / excluding content from the output data.
- [zemistr] Added fix for input data. (JSON string / Javascript object)
- New pickup of repo for developement.
- [tchapi] Merge Craig Sansam' branch https://github.com/craigsansam/Nestable/ - Add the noChildrenClass option
- [tchapi] Replace previous
changebehaviour with a callback
- Merge fix from [jails] : Fix change event triggered twice.
- [dbushell] add no-drag class for handle contents
- [dbushell] use
el.closestinstead ofel.parents - [dbushell] fix scroll offset on document.elementFromPoint()
- Merge for Zepto.js support
- Merge fix for remove/detach items
- Added
maxDepthoption (default to 5) - Added empty placeholder
- Updated CSS class structure with options for
listClassanditemClass. - Fixed to allow drag and drop between multiple Nestable instances (off by default).
- Added
groupoption to enabled the above.
Original Author: David Bushell http://dbushell.com @dbushell
New Author : Ramon Smit http://ramonsmit.nl @Ram0nSm1t
Contributors :
- Cyril http://tchap.me, Craig Sansam
- Zemistr http://zemistr.eu, Martin Zeman
Copyright © 2012 David Bushell / © Ramon Smit 2014 | BSD & MIT license