diff --git a/example/example.html b/example/example.html index 79b2de8..ec62237 100644 --- a/example/example.html +++ b/example/example.html @@ -38,13 +38,13 @@

Example 2


diff --git a/src/select2totree.js b/src/select2totree.js index 11dfa0d..03d1741 100644 --- a/src/select2totree.js +++ b/src/select2totree.js @@ -10,6 +10,10 @@ buildSelect(opts.treeData, this); } + if (opts.expandSelectedItemParents) { + var selectionChain=updateSelectionChain(this); + } + opts._templateResult = opts.templateResult; opts.templateResult = function (data, container) { var label = data.text; @@ -24,6 +28,12 @@ if (ele.getAttribute("data-pup")) { container.setAttribute("data-pup", ele.getAttribute("data-pup")); } + if (opts.expandSelectedItemParents && (selectionChain.indexOf(ele.value)>-1)) { + $(container).addClass("showme"); + if ($(container).hasClass("non-leaf") && selectionChain.indexOf(ele.value)!==0) { + $(container).addClass("opened"); + } + } if ($(container).hasClass("non-leaf")) { return $.merge($(''), $iteme); } @@ -48,6 +58,10 @@ $allsch.off("input", inputHandler); $allsch.on("input", inputHandler); }); + + s2inst.on("select2:select", function (evt) { + selectionChain=updateSelectionChain(this); + }); /* Show search result options even if they are collapsed */ function inputHandler(evt) { @@ -161,4 +175,15 @@ showHideSub(this); }); } + function updateSelectionChain(sl2) { + + var values=[]; + var currElm=$(sl2).find('option[value='+$(sl2).val()+']'); + do + { + values.push(currElm.val()); + } + while((currElm=$(sl2).find('option[value='+currElm.data('pup')+']')).length); + return values; + } })(jQuery);