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
+53-11Lines changed: 53 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,10 @@ <h2 id="data">
4
4
</h2>
5
5
6
6
<p>
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.
7
+
While Select2 is designed to be used with a <code><select></code> tag
8
+
the data that is used to search through and display the results can be
9
+
loaded from a JavaScript array using the <code>data</code> option. This
10
+
option should be passed in during the initialization of Select2.
8
11
</p>
9
12
10
13
{% highlight js linenos %}
@@ -24,15 +27,22 @@ <h3>
24
27
</h3>
25
28
26
29
<p>
27
-
The <code>id</code> and <code>text</code> properties are required on each object, and these are the properties that Select2 uses for the internal data objects. Any additional paramters passed in with data objects will be included on the data objects that Select2 exposes.
30
+
The <code>id</code> and <code>text</code> properties are required on each
31
+
object, and these are the properties that Select2 uses for the internal
32
+
data objects. Any additional paramters passed in with data objects will be
33
+
included on the data objects that Select2 exposes.
28
34
</p>
29
35
30
36
<h3>
31
37
How should nested results be formatted?
32
38
</h3>
33
39
34
40
<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.
41
+
Nested results should be specified using the <code>children</code> property
42
+
on the data objects that are passed in. This <code>children</code> property
43
+
should be an array of data objects that are grouped under this option, and
44
+
the label for the group should be specified as the <code>text</code>
45
+
property on the root data object.
36
46
</p>
37
47
38
48
{% highlight js linenos %}
@@ -53,36 +63,68 @@ <h3>
53
63
</h3>
54
64
55
65
<p>
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.
66
+
Because Select2 falls back to an <code><optgroup></code> when
67
+
creating nested options, only
68
+
<ahref="#how-many-levels-of-nesting-can-there-be">a single level of nesting</a>
69
+
is supported. Any additional levels of nesting is not guarenteed to be
70
+
displayed properly across all browsers and devices.
57
71
</p>
58
72
59
73
<h3>
60
74
Why are <code><option></code> tags being created?
61
75
</h3>
62
76
63
77
<p>
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.
78
+
The <code>data</code> option is a shortcut that Select2 provides which
79
+
allows you to load options into your <code>select</code> from a data array.
65
80
</p>
66
81
67
82
{% include options/not-written.html %}
68
83
69
84
<h3>
70
-
My objects don't use <code>id</code> for their unique identifiers, what can I do?
85
+
My objects don't use <code>id</code> for their unique identifiers,
86
+
what can I do?
71
87
</h3>
72
88
73
89
<p>
74
-
You can re-map your identifier before passing it to Select2.
90
+
Select2 requires that the <code>id</code> property is used to uniquely
91
+
identify the options that are displayed in the results list. If you use a
92
+
property other than <code>id</code> (like <code>pk</code>) to uniquely
93
+
identify an option, you need to map your old property to <code>id</code>
94
+
before passing it to Select2.
75
95
</p>
76
96
77
-
{% include options/not-written.html %}
97
+
<p>
98
+
If you cannot do this on your server or you are in a situation where the
99
+
identifier cannot be changed, you can do this in JavaScript before passing
100
+
it to Select2.
101
+
</p>
102
+
103
+
{% highlight js linenos %}
104
+
var data = $.map(yourArrayData, function (obj) {
105
+
obj.id = obj.id || obj.pk; // replace pk with your identifier
106
+
107
+
return obj;
108
+
});
109
+
{% endhighlight %}
78
110
79
111
<h3>
80
-
My objects use a property other than <code>text</code> for the text that needs to be displayed
112
+
My objects use a property other than <code>text</code> for the text that
113
+
needs to be displayed
81
114
</h3>
82
115
83
116
<p>
84
-
These can also be re-mapped.
117
+
Just like with the <code>id</code> property, Select2 requires that the text
118
+
that should be displayed for an option is stored in the <code>text</code>
119
+
property. You can map this property from any existing property using the
120
+
following JavaScript.
85
121
</p>
86
122
87
-
{% include options/not-written.html %}
123
+
{% highlight js linenos %}
124
+
var data = $.map(yourArrayData, function (obj) {
125
+
obj.text = obj.text || obj.name; // replace name with the property used for the text
0 commit comments