From f8252af7c8db344f7850cba4dbd7ecadf5c47cf6 Mon Sep 17 00:00:00 2001 From: Islam Date: Mon, 22 Feb 2021 14:08:14 +0200 Subject: [PATCH 1/3] Introduce 'updateSelectionChain' function to detect all 'option' element which need to be shown or opened, Auto open parent non-leaf options if 'expandSelectedItemParents' in set to 'true' in config of initialization --- src/select2totree.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/select2totree.js b/src/select2totree.js index 11dfa0d..0c581b4 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")) { + $(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); From 8fd36ae22221878992496e2b0685714bb1682276 Mon Sep 17 00:00:00 2001 From: Islam Date: Mon, 22 Feb 2021 14:09:18 +0200 Subject: [PATCH 2/3] Tune example #2 to use 'Expand Selected Item Parents' feature --- example/example.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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


From 32371d69f1b5d223b7ea47d5eb4490bdbbfc4715 Mon Sep 17 00:00:00 2001 From: Islam Date: Sun, 28 Feb 2021 21:50:34 +0200 Subject: [PATCH 3/3] Fix problem of expanding non-leaf option if it is the selected value --- src/select2totree.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/select2totree.js b/src/select2totree.js index 0c581b4..03d1741 100644 --- a/src/select2totree.js +++ b/src/select2totree.js @@ -30,7 +30,7 @@ } if (opts.expandSelectedItemParents && (selectionChain.indexOf(ele.value)>-1)) { $(container).addClass("showme"); - if ($(container).hasClass("non-leaf")) { + if ($(container).hasClass("non-leaf") && selectionChain.indexOf(ele.value)!==0) { $(container).addClass("opened"); } }