You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_includes/options/data/array.html
+37-2Lines changed: 37 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,21 @@ <h2 id="data">
4
4
</h2>
5
5
6
6
<p>
7
-
Yes, but only when you are initially creating it.
7
+
While Select2 is designed to be used with a <code><select></code> tag, the data that is used to search through and display the results can be loaded from a JavaScript array using the <code>data</code> option. This option should be passed in during the initialization of Select2.
8
8
</p>
9
9
10
+
<preclass="prettyprint linenums">
11
+
$('select').select2({
12
+
data: [
13
+
{
14
+
id: 'value',
15
+
text: 'Text to display'
16
+
},
17
+
// ... more data objects ...
18
+
]
19
+
});
20
+
</pre>
21
+
10
22
<h3>
11
23
What properties are required on the objects passed in to the array?
12
24
</h3>
@@ -19,12 +31,29 @@ <h3>
19
31
How should nested results be formatted?
20
32
</h3>
21
33
34
+
<p>
35
+
Nested results should be specified using the <code>children</code> property on the data objects that are passed in. This <code>children</code> property should be an array of data objects that are grouped under this option, and the label for the group should be specified as the <code>text</code> property on the root data object.
36
+
</p>
37
+
38
+
<preclass="prettyprint linenums">
39
+
{
40
+
text: 'Group label',
41
+
children: [
42
+
{
43
+
id: 'nested-1',
44
+
text: 'First nested option'
45
+
},
46
+
// ... more data objects ...
47
+
]
48
+
}
49
+
</pre>
50
+
22
51
<h3>
23
52
How many levels of nesting are allowed?
24
53
</h3>
25
54
26
55
<p>
27
-
Because Select2 falls back to an <code><optgroup></code> when creating nested options, only a single level of nesting can be supported.
56
+
Because Select2 falls back to an <code><optgroup></code> when creating nested options, only <ahref="#how-many-levels-of-nesting-can-there-be">a single level of nesting</a> is supported. Any additional levels of nesting is not guarenteed to be displayed properly across all browsers and devices.
28
57
</p>
29
58
30
59
<h3>
@@ -35,6 +64,8 @@ <h3>
35
64
The <code>data</code> option is a shortcut that Select2 provides which allows you to load options into your <code>select</code> from a data array.
36
65
</p>
37
66
67
+
{% include options/not-written.html %}
68
+
38
69
<h3>
39
70
My objects don't use <code>id</code> for their unique identifiers, what can I do?
40
71
</h3>
@@ -43,11 +74,15 @@ <h3>
43
74
You can re-map your identifier before passing it to Select2.
44
75
</p>
45
76
77
+
{% include options/not-written.html %}
78
+
46
79
<h3>
47
80
My objects use a property other than <code>text</code> for the text that needs to be displayed
Can Select2 be used with a <code><select></code> tag?
4
4
</h2>
5
5
6
+
<p>
7
+
Select2 was designed to be a replacement for the standard <code><select></code> boxes that are displayed by the browser, so by default it supports all options and operations that are available in a standard select box, but with added flexibility. There is no special configuration required to make Select2 work with a <code><select></code> tag.
8
+
</p>
9
+
6
10
<h3>
7
11
Does Select2 support nesting options?
8
12
</h3>
9
13
10
14
<p>
11
-
Yes, just like in a standard <code>select</code>.
15
+
A standard <code><select></code> box can display nested options by wrapping them with in an <code><optgroup></code> tag.
12
16
</p>
13
17
18
+
<preclass="prettyprint">
19
+
<select>
20
+
<optgroup label="Group Name">
21
+
<option>Nested option</option>
22
+
</optgroup>
23
+
</select>
24
+
</pre>
25
+
14
26
<h3>
15
27
How many levels of nesting can there be?
16
28
</h3>
17
29
18
30
<p>
19
-
Only a single level of nesting is allowed per the HTML specification.
31
+
Only a single level of nesting is allowed per the HTML specification. If you nest an <code><optgroup></code> within another <code><optgroup></code>, Select2 will not be able to detect the extra level of nesting and errors may be triggered as a result.
20
32
</p>
21
33
22
34
<h3>
23
35
Can <code><optgroup></code> tags be made selectable?
24
36
</h3>
25
37
26
38
<p>
27
-
No. This is a limitation of the HTML specification and is not a limitation that Select2 can overcome.
39
+
No. This is a limitation of the HTML specification and is not a limitation that Select2 can overcome. You can emulate grouping by using an <code><option></code> instead of an <code><optgroup></code> and <ahref="http://stackoverflow.com/q/30820215/359284#30948247">changing the style by using CSS</a>, but this is not recommended as it is not fully accessible.
0 commit comments