Skip to content

Commit 55aa2c6

Browse files
committed
More docs for data adapters
1 parent d6bc96d commit 55aa2c6

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

docs/_includes/options/data/array.html

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@ <h2 id="data">
44
</h2>
55

66
<p>
7-
Yes, but only when you are initially creating it.
7+
While Select2 is designed to be used with a <code>&lt;select&gt;</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.
88
</p>
99

10+
<pre class="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+
1022
<h3>
1123
What properties are required on the objects passed in to the array?
1224
</h3>
@@ -19,12 +31,29 @@ <h3>
1931
How should nested results be formatted?
2032
</h3>
2133

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+
<pre class="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+
2251
<h3>
2352
How many levels of nesting are allowed?
2453
</h3>
2554

2655
<p>
27-
Because Select2 falls back to an <code>&lt;optgroup&gt;</code> when creating nested options, only a single level of nesting can be supported.
56+
Because Select2 falls back to an <code>&lt;optgroup&gt;</code> when creating nested options, only <a href="#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.
2857
</p>
2958

3059
<h3>
@@ -35,6 +64,8 @@ <h3>
3564
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.
3665
</p>
3766

67+
{% include options/not-written.html %}
68+
3869
<h3>
3970
My objects don&apos;t use <code>id</code> for their unique identifiers, what can I do?
4071
</h3>
@@ -43,11 +74,15 @@ <h3>
4374
You can re-map your identifier before passing it to Select2.
4475
</p>
4576

77+
{% include options/not-written.html %}
78+
4679
<h3>
4780
My objects use a property other than <code>text</code> for the text that needs to be displayed
4881
</h3>
4982

5083
<p>
5184
These can also be re-mapped.
5285
</p>
86+
87+
{% include options/not-written.html %}
5388
</section>

docs/_includes/options/data/select.html

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,40 @@ <h2 id="data-adapters-select-tag">
33
Can Select2 be used with a <code>&lt;select&gt;</code> tag?
44
</h2>
55

6+
<p>
7+
Select2 was designed to be a replacement for the standard <code>&lt;select&gt;</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>&lt;select&gt;</code> tag.
8+
</p>
9+
610
<h3>
711
Does Select2 support nesting options?
812
</h3>
913

1014
<p>
11-
Yes, just like in a standard <code>select</code>.
15+
A standard <code>&lt;select&gt;</code> box can display nested options by wrapping them with in an <code>&lt;optgroup&gt;</code> tag.
1216
</p>
1317

18+
<pre class="prettyprint">
19+
&lt;select&gt;
20+
&lt;optgroup label="Group Name"&gt;
21+
&lt;option&gt;Nested option&lt;/option&gt;
22+
&lt;/optgroup&gt;
23+
&lt;/select&gt;
24+
</pre>
25+
1426
<h3>
1527
How many levels of nesting can there be?
1628
</h3>
1729

1830
<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>&lt;optgroup&gt;</code> within another <code>&lt;optgroup&gt;</code>, Select2 will not be able to detect the extra level of nesting and errors may be triggered as a result.
2032
</p>
2133

2234
<h3>
2335
Can <code>&lt;optgroup&gt;</code> tags be made selectable?
2436
</h3>
2537

2638
<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>&lt;option&gt;</code> instead of an <code>&lt;optgroup&gt;</code> and <a href="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.
2840
</p>
2941

3042
<h3>

0 commit comments

Comments
 (0)