Skip to content

Commit ac10968

Browse files
committed
Docs for mapping id and text
These modified snippets are from the announcement. This closes select2#4086.
1 parent 6369f5f commit ac10968

1 file changed

Lines changed: 53 additions & 11 deletions

File tree

docs/_includes/options/data/array.html

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ <h2 id="data">
44
</h2>
55

66
<p>
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.
7+
While Select2 is designed to be used with a <code>&lt;select&gt;</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.
811
</p>
912

1013
{% highlight js linenos %}
@@ -24,15 +27,22 @@ <h3>
2427
</h3>
2528

2629
<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.
2834
</p>
2935

3036
<h3>
3137
How should nested results be formatted?
3238
</h3>
3339

3440
<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.
3646
</p>
3747

3848
{% highlight js linenos %}
@@ -53,36 +63,68 @@ <h3>
5363
</h3>
5464

5565
<p>
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.
66+
Because Select2 falls back to an <code>&lt;optgroup&gt;</code> when
67+
creating nested options, only
68+
<a href="#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.
5771
</p>
5872

5973
<h3>
6074
Why are <code>&lt;option&gt;</code> tags being created?
6175
</h3>
6276

6377
<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.
6580
</p>
6681

6782
{% include options/not-written.html %}
6883

6984
<h3>
70-
My objects don&apos;t use <code>id</code> for their unique identifiers, what can I do?
85+
My objects don&apos;t use <code>id</code> for their unique identifiers,
86+
what can I do?
7187
</h3>
7288

7389
<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.
7595
</p>
7696

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 %}
78110

79111
<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
81114
</h3>
82115

83116
<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.
85121
</p>
86122

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
126+
127+
return obj;
128+
});
129+
{% endhighlight %}
88130
</section>

0 commit comments

Comments
 (0)