Nestable2 - new pickup of Nestable!
========
### Drag & drop hierarchical list with mouse and touch compatibility (jQuery / Zepto plugin)
[](https://ramonsmit.github.com/Nestable2/)
[](https://travis-ci.org/RamonSmit/Nestable2)
[](https://github.com/RamonSmit/Nestable2/blob/master/LICENSE)
[](https://www.npmjs.com/package/nestable2)
Nestable is an experimental example and IS under active development. If it suits your requirements feel free to expand upon it!
## Install
You can install this package either with `npm` or with `bower`.
### npm
```shell
npm install --save nestable2
```
Then add a `
```
Or `require('nestable2')` from your code.
### bower
```shell
bower install --save nestable2
```
### CDN
You can also find us on [CDNJS](https://cdnjs.com/libraries/nestable2):
```
//cdnjs.cloudflare.com/ajax/libs/nestable2/1.6.0/jquery.nestable.min.css
//cdnjs.cloudflare.com/ajax/libs/nestable2/1.6.0/jquery.nestable.min.js
```
## Usage
Write your nested HTML lists like so:
```html
Item 1
Item 2
Item 3
Item 4
Item 5
```
Then activate with jQuery like so:
```js
$('.dd').nestable({ /* config options */ });
```
### Events
`change`: For using an .on handler in jquery
The `callback` provided as an option is fired when elements are reordered or nested.
```js
$('.dd').nestable({
callback: function(l,e){
// l is the main container
// e is the element that was moved
}
});
```
`onDragStart` callback provided as an option is fired when user starts to drag an element. Returning `false` from this callback will disable the dragging.
```js
$('.dd').nestable({
onDragStart: function(l,e){
// l is the main container
// e is the element that was moved
}
});
```
This callback can be used to manipulate element which is being dragged as well as whole list.
For example you can conditionally add `.dd-nochildren` to forbid dropping current element to
some other elements for instance based on `data-type` of current element and other elements:
```js
$('.dd').nestable({
onDragStart: function (l, e) {
// get type of dragged element
var type = $(e).data('type');
// based on type of dragged element add or remove no children class
switch (type) {
case 'type1':
// element of type1 can be child of type2 and type3
l.find("[data-type=type2]").removeClass('dd-nochildren');
l.find("[data-type=type3]").removeClass('dd-nochildren');
break;
case 'type2':
// element of type2 cannot be child of type2 or type3
l.find("[data-type=type2]").addClass('dd-nochildren');
l.find("[data-type=type3]").addClass('dd-nochildren');
break;
case 'type3':
// element of type3 cannot be child of type2 but can be child of type3
l.find("[data-type=type2]").addClass('dd-nochildren');
l.find("[data-type=type3]").removeClass('dd-nochildren');
break;
default:
console.error("Invalid type");
}
}
});
```
`beforeDragStop` callback provided as an option is fired when user drop an element and before 'callback' method fired. Returning false from this callback will disable the dropping and restore element at start position.
```js
$('.dd').nestable({
beforeDragStop: function(l,e, p){
// l is the main container
// e is the element that was moved
// p is the place where element was moved.
}
});
```
### Methods
`serialize`:
You can get a serialised object with all `data-*` attributes for each item.
```js
$('.dd').nestable('serialize');
```
The serialised JSON for the example above would be:
```json
[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5,"foo":"bar"}]}]
```
`toArray`:
```js
$('.dd').nestable('toArray');
```
Builds an array where each element looks like:
```js
{
'depth': depth,
'id': id,
'left': left,
'parent_id': parentId || null,
'right': right
}
```
You can get a hierarchical nested set model like below.
```js
$('.dd').nestable('asNestedSet');
```
The output will be like below:
```js
[{"id":1,"parent_id":"","depth":0,"lft":1,"rgt":2},{"id":2,"parent_id":"","depth":0,"lft":3,"rgt":4},{"id":3,"parent_id":"","depth":0,"lft":5,"rgt":10},{"id":4,"parent_id":3,"depth":1,"lft":6,"rgt":7},{"id":5,"parent_id":3,"depth":1,"lft":8,"rgt":9}]
```
`add`:
You can add any item by passing an object. New item will be appended to the root tree.
```js
$('.dd').nestable('add', {"id":1,"children":[{"id":4}]});
```
Optionally you can set 'parent_id' property on your object and control in which place in tree your item will be added.
```js
$('.dd').nestable('add', {"id":1,"parent_id":2,"children":[{"id":4}]});
```
`replace`:
You can replace existing item in tree by passing an object with 'id' property.
```js
$('.dd').nestable('replace', {"id":1,"foo":"bar"});
```
You need to remember that if you're replacing item with children's you need to pass this children's in object as well.
```js
$('.dd').nestable('replace', {"id":1,"children":[{"id":4}]});
```
`remove`:
You can remove existing item by passing 'id' of this element. To animate item removing check `effect` config option. This will delete the item with all his children.
```js
$('.dd').nestable('remove', 1);
```
This will invoke callback function after deleting the item with data-id '1'.
```js
$('.dd').nestable('remove', 1, function(){
console.log('Item deleted');
});
```
`removeAll`:
Removes all items from the list. To animate items removing check `effect` config option. You can also use callback function to do something after removing all items.
```js
$('.dd').nestable('removeAll', function(){
console.log('All items deleted');
});
```
`destroy`:
You can deactivate the plugin by running
```js
$('.dd').nestable('destroy');
```
### Autoscroll while dragging
Autoscrolls the container element while dragging if you drag the element over the offsets defined in `scrollTriggers` config option.
```js
$('.dd').nestable({ scroll: true });
```
To use this feature you need to have `jQuery >= 1.9` and `scrollParent()` method.
You can be find this method in `jQuery UI` or if you don't want to have `jQuery UI` as a dependency you can use [this repository](https://github.com/slindberg/jquery-scrollparent).
You can also control the scroll sensitivity and speed, check `scrollSensitivity` and `scrollSpeed` options.
### On the fly nestable generation
You can passed serialized JSON as an option if you like to dynamically generate a Nestable list:
```html
```
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:
```html