From 04a29b8c25cad3639c7aa98cd77f705ff270b1bf Mon Sep 17 00:00:00 2001 From: Yuri Sarzi Date: Tue, 31 May 2016 15:44:12 +0200 Subject: [PATCH 1/6] Added 'add' method Use like: $('#nestable').nestable('add', { id: "item-ID", text: "Text here"} ); Object indexes "id" and "text" are mandatory, any extra index will be added as HTML5 "data-" attribute --- jquery.nestable.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/jquery.nestable.js b/jquery.nestable.js index 7323fb4..ad5e038 100644 --- a/jquery.nestable.js +++ b/jquery.nestable.js @@ -136,6 +136,24 @@ }, + add: function(el){ + if(!el.id || !el.text){ + return; + } + var list = this, + opt = list.options, + new_el = $('
  • '); + + new_el.find('.'+opt.handleClass).text(el.text); + new_el.attr('data-id', el.id); + for (var key in el) { + if (el.hasOwnProperty(key) && key != 'id' && key != 'text'){ + new_el.attr('data-'+key, el[key]); + } + } + list.el.find(opt.itemNodeName).last().after(new_el); + }, + serialize: function() { var data, @@ -473,7 +491,11 @@ $(this).data("nestable-id", new Date().getTime()); } else { if (typeof params === 'string' && typeof plugin[params] === 'function') { - retval = plugin[params](); + if (typeof val === 'object') { + retval = plugin[params](val); + }else{ + retval = plugin[params](); + } } } }); From e0c6a58f961e33d1c96096fb17fc19df9a32c84d Mon Sep 17 00:00:00 2001 From: Yuri Sarzi Date: Tue, 31 May 2016 15:59:01 +0200 Subject: [PATCH 2/6] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index b86e091..cb22d1f 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,16 @@ You can get a serialised object with all `data-*` attributes for each item. The serialised JSON for the example above would be: [{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}] + +You can add any item by passing an object. New item will be appended to the list. +The `id` and `text` indexes are mandatory, while any other index found will be added as `data-*` attribute + + $('.dd').nestable('add', {"id": "item-ID", "text": "Text Here"}); + $('.dd').nestable('add', {"id": "item-ID", "text": "Text Here", "attribute": "attr-value", ...}); + +This will result in the following serialized JSON: + + [{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}, {"id":"item-ID", "attribute": "attr-value"}] ### Configuration From a26dda94b014dca43b8bb9a79113b2694809628a Mon Sep 17 00:00:00 2001 From: Yuri Sarzi Date: Tue, 31 May 2016 16:00:34 +0200 Subject: [PATCH 3/6] Update jquery.nestable.js --- jquery.nestable.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/jquery.nestable.js b/jquery.nestable.js index ad5e038..383515a 100644 --- a/jquery.nestable.js +++ b/jquery.nestable.js @@ -142,9 +142,8 @@ } var list = this, opt = list.options, - new_el = $('
  • '); - - new_el.find('.'+opt.handleClass).text(el.text); + new_el = $('<'+opt.listNodeName+'>'); + new_el.addClass(opt.itemClass).append($('
    ').addClass(opt.handleClass).text(el.text)); new_el.attr('data-id', el.id); for (var key in el) { if (el.hasOwnProperty(key) && key != 'id' && key != 'text'){ From 679d8fe684f8bd24bfd7d09e9bdb530292610b18 Mon Sep 17 00:00:00 2001 From: Yuri Sarzi Date: Tue, 31 May 2016 16:08:13 +0200 Subject: [PATCH 4/6] Update jquery.nestable.js --- jquery.nestable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.nestable.js b/jquery.nestable.js index 383515a..28bb989 100644 --- a/jquery.nestable.js +++ b/jquery.nestable.js @@ -142,7 +142,7 @@ } var list = this, opt = list.options, - new_el = $('<'+opt.listNodeName+'>'); + new_el = $('<'+opt.itemNodeName+'>'); new_el.addClass(opt.itemClass).append($('
    ').addClass(opt.handleClass).text(el.text)); new_el.attr('data-id', el.id); for (var key in el) { From e8e4ec14832baf0b3052ee152c0fe457a5fa89c1 Mon Sep 17 00:00:00 2001 From: Yuri Sarzi Date: Fri, 3 Jun 2016 09:00:25 +0200 Subject: [PATCH 5/6] Added 'addChild' and 'remove' methods addChild triggere if added element has 'parent' index set --- jquery.nestable.js | 69 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/jquery.nestable.js b/jquery.nestable.js index 28bb989..7f21ca4 100644 --- a/jquery.nestable.js +++ b/jquery.nestable.js @@ -136,21 +136,66 @@ }, - add: function(el){ - if(!el.id || !el.text){ + + + add: function(el) + { + if(!el.id || !el.text) { return; } - var list = this, - opt = list.options, - new_el = $('<'+opt.itemNodeName+'>'); - new_el.addClass(opt.itemClass).append($('
    ').addClass(opt.handleClass).text(el.text)); - new_el.attr('data-id', el.id); - for (var key in el) { - if (el.hasOwnProperty(key) && key != 'id' && key != 'text'){ - new_el.attr('data-'+key, el[key]); + var list = this; + if(el.parent) { + list.addChild(el); + } + else { + var new_el = $('<'+list.options.itemNodeName+'>'); + new_el.addClass(list.options.itemClass).append($('
    ').addClass(list.options.handleClass).text(el.text)); + new_el.attr('data-id', el.id); + for (var key in el) { + if (el.hasOwnProperty(key) && key != 'id' && key != 'text') { + new_el.attr('data-'+key, el[key]); + } + } + list.el.children('.' + list.options.listClass).first().append(new_el); + } + }, + + addChild: function(child){ + var list = this; + var new_item = $('<'+list.options.itemNodeName+'>'); + new_item.addClass(list.options.itemClass).append($('
    ').addClass(list.options.handleClass).text(child.text)); + new_item.attr('data-id', child.id); + for (var key in child) { + if (child.hasOwnProperty(key) && key != 'id' && key != 'text'){ + new_item.attr('data-'+key, child[key]); } } - list.el.find(opt.itemNodeName).last().after(new_el); + $.each(list.el.find(list.options.itemNodeName), function(k, el) { + if($(el).attr('data-id') == child.parent){ + if($(el).children(list.options.listNodeName).length){ + $(el).find(list.options.listNodeName).append(new_item); + } + else{ + var new_list = $('<'+list.options.listNodeName+'>').addClass(list.options.listClass); + new_list.append(new_item); + $(el).append(new_list); + list.setParent($(el)); + } + return; + } + }); + + }, + + remove: function(id) + { + var list = this; + $.each(list.el.find(list.options.itemNodeName), function(k, el) { + if($(el).attr('data-id') == id){ + $(el).remove(); + return; + } + }); }, serialize: function() @@ -490,7 +535,7 @@ $(this).data("nestable-id", new Date().getTime()); } else { if (typeof params === 'string' && typeof plugin[params] === 'function') { - if (typeof val === 'object') { + if (typeof val !== 'undefined') { retval = plugin[params](val); }else{ retval = plugin[params](); From 6312d24dc5ffdea51c26b3c87639ab533a2e7e70 Mon Sep 17 00:00:00 2001 From: Yuri Sarzi Date: Thu, 13 Apr 2017 21:49:17 +0200 Subject: [PATCH 6/6] Undefined el fix Undefined element el error should be now fixed. --- jquery.nestable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.nestable.js b/jquery.nestable.js index 7f21ca4..c2b9540 100644 --- a/jquery.nestable.js +++ b/jquery.nestable.js @@ -140,7 +140,7 @@ add: function(el) { - if(!el.id || !el.text) { + if(!el || !el.id || !el.text) { return; } var list = this;