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

Commit 8a2c3c8

Browse files
author
clivezhg
authored
Merge pull request #21 from clivezhg/cli_work
The release 1.2.0
2 parents 833ea98 + d68ccf7 commit 8a2c3c8

File tree

4 files changed

+72
-72
lines changed

4 files changed

+72
-72
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ About the data structure: "`id`" will be used as option value, "`text`" will be
6767
- `labelFld`, the option label field, it's "`text`" by default.
6868
- `incFld`, the sub options field, it's "`inc`" by default.
6969
- `dftVal`, the default value.
70+
For `valFld` and `labelFld`, you can give a object path (eg: `item.label`. see "Example 4" in "example/example.html").
7071

7172
For `valFld` and `labelFld` you can give a object path (ex: `item.label`), we split by `.`.
7273

example/example.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,30 @@ <h3>Example 3</h3>
7171
else return state.text;
7272
};
7373
</script>
74+
75+
<br>
76+
77+
<h3>Example 4</h3>
78+
<select id="sel_4" style="width:16em" multiple>
79+
</select>
80+
<script>
81+
var mydata_path = [
82+
{"organization": {"shortDescription":"The company A", "idCompanyOrganization":"1"}, "nodes":[
83+
{"organization": {"shortDescription":"R & D", "idCompanyOrganization":"1_1"}, "nodes":[]
84+
},
85+
{"organization": {"shortDescription":"Marketing", "idCompanyOrganization":"1_2"}, "nodes":[
86+
{"organization": {"shortDescription":"Asia", "idCompanyOrganization":"1_2_1"}, "nodes":[
87+
{"organization": {"shortDescription":"South-east Asia", "idCompanyOrganization":"1_2_2"}, "nodes":[]
88+
}
89+
]
90+
}
91+
]
92+
}
93+
]
94+
}
95+
];
96+
97+
$("#sel_4").select2ToTree({treeData: {dataArr: mydata_path, valFld: "organization.idCompanyOrganization", labelFld: "organization.shortDescription", incFld:'nodes'}, maximumSelectionLength: 3});
98+
</script>
7499
</body>
75100
</html>

src/select2totree.css

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,119 +6,78 @@
66
box-sizing: border-box;
77
}
88

9-
.s2-to-tree .select2-dropdown[dir*="ltr"] .select2-results__option.l1 {
9+
.s2-to-tree .select2-results__option.l1 {
1010
margin-left: 0.6em;
1111
font-size: 1em;
1212
}
13-
.s2-to-tree .select2-dropdown[dir*="ltr"] .select2-results__option.l2 {
13+
.s2-to-tree .select2-results__option.l2 {
1414
margin-left: 1.4em;
1515
font-size: 0.95em;
1616
}
17-
.s2-to-tree .select2-dropdown[dir*="ltr"] .select2-results__option.l3 {
17+
.s2-to-tree .select2-results__option.l3 {
1818
margin-left: 2.2em;
1919
font-size: 0.91em;
2020
}
21-
.s2-to-tree .select2-dropdown[dir*="ltr"] .select2-results__option.l4 {
21+
.s2-to-tree .select2-results__option.l4 {
2222
margin-left: 3.0em;
2323
font-size: 0.87em;
2424
}
25-
.s2-to-tree .select2-dropdown[dir*="ltr"] .select2-results__option.l5 {
25+
.s2-to-tree .select2-results__option.l5 {
2626
margin-left: 3.8em;
2727
font-size: 0.83em;
2828
}
29-
.s2-to-tree .select2-dropdown[dir*="ltr"] .select2-results__option.l6 {
29+
.s2-to-tree .select2-results__option.l6 {
3030
margin-left: 4.5em;
3131
font-size: 0.8em;
3232
}
33-
.s2-to-tree .select2-dropdown[dir*="ltr"] .select2-results__option.l7 {
33+
.s2-to-tree .select2-results__option.l7 {
3434
margin-left: 5.3em;
3535
font-size: 0.77em;
3636
}
37-
.s2-to-tree .select2-dropdown[dir*="ltr"] .select2-results__option.l8 {
37+
.s2-to-tree .select2-results__option.l8 {
3838
margin-left: 6.0em;
3939
font-size: 0.75em;
4040
}
4141

42-
.s2-to-tree .select2-dropdown[dir*="ltr"] .item-label {
43-
display: inline-block;
44-
margin-left: 0.5em;
45-
padding: 3px;
46-
width: calc(100% - 11px); /* IE8 will fit the content */
47-
}
48-
49-
.s2-to-tree .select2-dropdown[dir*="ltr"] li.select2-results__option.non-leaf .expand-collapse:before { /* no :: in IE8 */
50-
content: "+";
51-
position: absolute;
52-
left: -0.35em;
53-
top: 0.1em;
54-
font-size: larger;
55-
}
56-
57-
58-
59-
60-
61-
62-
6342
.s2-to-tree .select2-dropdown[dir*="rtl"] .select2-results__option.l1 {
6443
margin-right: 0.6em;
65-
font-size: 1em;
6644
}
6745
.s2-to-tree .select2-dropdown[dir*="rtl"] .select2-results__option.l2 {
6846
margin-right: 1.4em;
69-
font-size: 0.95em;
7047
}
7148
.s2-to-tree .select2-dropdown[dir*="rtl"] .select2-results__option.l3 {
7249
margin-right: 2.2em;
73-
font-size: 0.91em;
7450
}
7551
.s2-to-tree .select2-dropdown[dir*="rtl"] .select2-results__option.l4 {
7652
margin-right: 3.0em;
77-
font-size: 0.87em;
7853
}
7954
.s2-to-tree .select2-dropdown[dir*="rtl"] .select2-results__option.l5 {
8055
margin-right: 3.8em;
81-
font-size: 0.83em;
8256
}
8357
.s2-to-tree .select2-dropdown[dir*="rtl"] .select2-results__option.l6 {
8458
margin-right: 4.5em;
85-
font-size: 0.8em;
8659
}
8760
.s2-to-tree .select2-dropdown[dir*="rtl"] .select2-results__option.l7 {
8861
margin-right: 5.3em;
89-
font-size: 0.77em;
9062
}
9163
.s2-to-tree .select2-dropdown[dir*="rtl"] .select2-results__option.l8 {
9264
margin-right: 6.0em;
93-
font-size: 0.75em;
9465
}
9566

96-
.s2-to-tree .select2-dropdown[dir*="rtl"] .item-label {
97-
display: inline-block;
98-
margin-right: 0.5em;
99-
padding: 3px;
100-
width: calc(100% - 11px); /* IE8 will fit the content */
67+
.s2-to-tree li.select2-results__option.non-leaf .expand-collapse:hover {
68+
color: red;
69+
cursor: pointer;
10170
}
102-
103-
.s2-to-tree .select2-dropdown[dir*="rtl"] li.select2-results__option.non-leaf .expand-collapse:before { /* no :: in IE8 */
71+
.s2-to-tree li.select2-results__option.non-leaf .expand-collapse:before { /* no :: in IE8 */
10472
content: "+";
10573
position: absolute;
106-
right: -0.35em;
74+
left: -0.35em;
10775
top: 0.1em;
10876
font-size: larger;
10977
}
110-
111-
112-
113-
114-
115-
116-
117-
.s2-to-tree li.select2-results__option.non-leaf .expand-collapse:hover {
118-
color: red;
119-
cursor: pointer;
78+
.s2-to-tree .select2-dropdown[dir*="rtl"] li.select2-results__option.non-leaf .expand-collapse:before {
79+
right: -0.35em;
12080
}
121-
12281
.s2-to-tree li.select2-results__option.non-leaf.opened .expand-collapse:before {
12382
content: "−";
12483
}
@@ -127,6 +86,16 @@
12786
color: black;
12887
}
12988

89+
.s2-to-tree .item-label {
90+
display: inline-block;
91+
margin-left: 0.5em;
92+
padding: 3px;
93+
width: calc(100% - 11px); /* IE8 will fit the content */
94+
}
95+
.s2-to-tree .select2-dropdown[dir*="rtl"] .item-label {
96+
margin-right: 0.5em;
97+
}
98+
13099
.s2-to-tree li.select2-results__option {
131100
position: relative;
132101
padding: 0px;

src/select2totree.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,23 @@
6363
return s2inst;
6464
};
6565

66-
function readPath(object, path){
67-
var currentPosition = object;
68-
for (var j = 0; j < path.length; j++) {
69-
var currentPath = path[j];
70-
if (currentPosition[currentPath]){
71-
currentPosition = currentPosition[currentPath];
72-
continue;
66+
/* Build the Select Option elements */
67+
function buildSelect(treeData, $el) {
68+
69+
/* Support the object path (eg: `item.label`) for 'valFld' & 'labelFld' */
70+
function readPath(object, path) {
71+
var currentPosition = object;
72+
for (var j = 0; j < path.length; j++) {
73+
var currentPath = path[j];
74+
if (currentPosition[currentPath]) {
75+
currentPosition = currentPosition[currentPath];
76+
continue;
77+
}
78+
return 'MISSING';
7379
}
74-
return;
80+
return currentPosition;
7581
}
76-
return currentPosition;
77-
}
7882

79-
function buildSelect(treeData, $el) { // build Select options according to Select-to-Tree specification
8083
function buildOptions(dataArr, curLevel, pup) {
8184
var labelPath;
8285
if (treeData.labelFld && treeData.labelFld.split('.').length> 1){
@@ -86,12 +89,13 @@
8689
if (treeData.valFld && treeData.valFld.split('.').length > 1) {
8790
idPath = treeData.valFld.split('.');
8891
}
92+
8993
for (var i = 0; i < dataArr.length; i++) {
9094
var data = dataArr[i] || {};
9195
var $opt = $("<option></option>");
92-
if (labelPath){
93-
$opt.text(readPath(data, labelPath));
94-
}else {
96+
if (labelPath) {
97+
$opt.text(readPath(data, labelPath));
98+
} else {
9599
$opt.text(data[treeData.labelFld || "text"]);
96100
}
97101
if (idPath) {
@@ -114,8 +118,9 @@
114118
$opt.addClass("non-leaf");
115119
buildOptions(inc, curLevel+1, $opt.val());
116120
}
117-
}
118-
}
121+
} // end 'for'
122+
} // end 'buildOptions'
123+
119124
buildOptions(treeData.dataArr, 1, "");
120125
if (treeData.dftVal) $el.val(treeData.dftVal);
121126
}

0 commit comments

Comments
 (0)