Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Commit ec483f2

Browse files
author
clivezhg
committed
the fix of 'templateResult' issue, & example change
1 parent d198cb1 commit ec483f2

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

example/1.png

609 Bytes
Loading

example/2.png

503 Bytes
Loading

example/3.png

472 Bytes
Loading

example/example.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,19 @@ <h3>Example 3</h3>
5757
ind.inc = [{text:"north", inc:[]}];
5858
var inc = ind.inc[0].inc;
5959
for (var i = 1; i <= 1500; i++) {
60-
inc.push({ id: 1000000+i, text: "india city "+i });
60+
inc.push({ id: 10000+i, text: "the city "+i });
6161
}
62-
$("#sel_3").select2ToTree({treeData: {dataArr: mydata}, maximumSelectionLength: 3});
62+
$("#sel_3").select2ToTree({treeData: {dataArr: mydata}, maximumSelectionLength: 3,
63+
templateResult: formatState, templateSelection: formatState});
64+
65+
function formatState (state) {
66+
if (state.id >= 1 && state.id <= 3) {
67+
return $(
68+
'<span><img src="./' + state.element.value.toLowerCase() + '.png" class="img-flag" /> ' + state.text + '</span>'
69+
);
70+
}
71+
else return state.text;
72+
};
6373
</script>
6474
</body>
6575
</html>

src/select2totree.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Select2-to-Tree CSS 1.0.0
2+
* Select2-to-Tree CSS 1.1.0
33
* https://github.com/clivezhg/select2-to-tree
44
*/
55
.s2-to-tree * {

src/select2totree.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* Select2-to-Tree 1.0.0
2+
* Select2-to-Tree 1.1.0
33
* https://github.com/clivezhg/select2-to-tree
44
*/
55
(function ($) {
@@ -10,8 +10,13 @@
1010
buildSelect(opts.treeData, this);
1111
}
1212

13+
opts.tr_old = opts.templateResult;
1314
opts.templateResult = function (data, container) {
14-
var $iteme = $("<span class='item-label'></span>").text(data.text);
15+
var label = data.text;
16+
if (typeof opts.tr_old === "function") {
17+
label = opts.tr_old(data, container);
18+
}
19+
var $iteme = $("<span class='item-label'></span>").append(label);
1520
if (data.element) {
1621
var ele = data.element;
1722
container.setAttribute("data-val", ele.value);

0 commit comments

Comments
 (0)