Skip to content

Commit

Permalink
Merge pull request #63 from pjona/function_add
Browse files Browse the repository at this point in the history
Added `add` function
  • Loading branch information
RamonSmit authored May 24, 2017
2 parents bf0881e + 8371e00 commit 38719be
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 37 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ The output will be like below:
[{"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 list.
```js
$('.dd').nestable('add', {"id":1,"children":[{"id":4}]});
```

`destroy`:
You can deactivate the plugin by running
```js
Expand All @@ -178,7 +184,7 @@ You can passed serialized JSON as an option if you like to dynamically generate

<script>
var json = '[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5,"foo":"bar"}]}]';
var options = {'json': json }
var options = {'json': json}
$('#nestable-json').nestable(options);
</script>
```
Expand Down Expand Up @@ -265,6 +271,7 @@ These advanced config options are also available:
### 22th May 2017

* [pjona] Added npm installation
* [pjona] Added `add` function

### 10th April 2017

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestable2",
"version": "1.2.4",
"version": "1.2.5",
"homepage": "https://github.com/RamonSmit/Nestable",
"authors": [
"Ramon Smit <@_RamonSmit>",
Expand Down
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ <h1>Nestable</h1>
<menu id="nestable-menu">
<button type="button" data-action="expand-all">Expand All</button>
<button type="button" data-action="collapse-all">Collapse All</button>
<button type="button" data-action="add-item">Add new item</button>
</menu>

<div class="cf nestable-lists">
Expand Down Expand Up @@ -384,6 +385,7 @@ <h1>Nestable</h1>
]
}
];
var lastId = 12;

// activate Nestable for list 1
$('#nestable').nestable({
Expand Down Expand Up @@ -415,6 +417,9 @@ <h1>Nestable</h1>
if(action === 'collapse-all') {
$('.dd').nestable('collapseAll');
}
if(action === 'add-item') {
$('#nestable').nestable('add', {"id": ++lastId, "content": "Item " + lastId});
}
});

$('#nestable3').nestable();
Expand Down
74 changes: 42 additions & 32 deletions jquery.nestable.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,38 @@
this.el.trigger('destroy-nestable');
},

add: function (item)
{
var html = this._buildItem(item, this.options);
$(this.el).children('.' + this.options.listClass).append(html);
},

_build: function() {
var json = this.options.json;

if(typeof json === 'string') {
json = JSON.parse(json);
}

$(this.el).html(this._buildList(json, this.options));
},

_buildList: function(items, options) {
if(!items) {
return '';
}

var children = '';
var that = this;

$.each(items, function(index, sub) {
children += that._buildItem(sub, options);
});

return options.listRenderer(children, options);
},

_buildItem: function(item, options) {
function escapeHtml(text) {
var map = {
'&': '&amp;',
Expand Down Expand Up @@ -274,37 +305,13 @@
return data_attrs;
}

function buildList(items, options) {
if(!items) {
return '';
}

var children = '';

$.each(items, function(index, sub) {
children += buildItem(sub, options);
});

return options.listRenderer(children, options);
}

function buildItem(item, options) {
var item_attrs = createDataAttrs(item);
item_attrs["class"] = createClassesString(item, options);
var item_attrs = createDataAttrs(item);
item_attrs["class"] = createClassesString(item, options);

var content = options.contentCallback(item);
var children = buildList(item.children, options);
var content = options.contentCallback(item);
var children = this._buildList(item.children, options);

return options.itemRenderer(item_attrs, content, children, options, item);
}

var json = this.options.json;

if(typeof json == 'string') {
json = JSON.parse(json);
}

$(this.el).html(buildList(json, this.options));
return options.itemRenderer(item_attrs, content, children, options, item);
},

serialize: function() {
Expand Down Expand Up @@ -438,7 +445,6 @@
if (item.children(o.options.listNodeName).children(o.options.itemNodeName).length > 0) {
depth++;
item.children(o.options.listNodeName).children(o.options.itemNodeName).each(function() {
//console.log($(this));
right = _recursiveArray($(this), depth, right);
});
depth--;
Expand Down Expand Up @@ -822,7 +828,7 @@

};

$.fn.nestable = function(params) {
$.fn.nestable = function(params, val) {
var lists = this,
retval = this;

Expand All @@ -842,7 +848,11 @@
}
else {
if(typeof params === 'string' && typeof plugin[params] === 'function') {
retval = plugin[params]();
if (typeof val !== 'undefined') {
retval = plugin[params](val);
}else{
retval = plugin[params]();
}
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestable2",
"version": "1.2.3",
"version": "1.2.5",
"description": "Drag & drop hierarchical list with mouse and touch compatibility",
"main": "jquery.nestable.js",
"scripts": {
Expand Down Expand Up @@ -34,9 +34,9 @@
"email": "[email protected]"
}
],
"dependencies": [
"dependencies": {
"jquery": "^1.7.2"
],
},
"license": "MIT",
"bugs": {
"url": "https://github.com/RamonSmit/Nestable/issues"
Expand Down

0 comments on commit 38719be

Please sign in to comment.