Skip to content

Commit 576f937

Browse files
senotrusovjpic
authored andcommitted
Add documentation on how to programmatically access a selection data.
1 parent 8dc02bc commit 576f937

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

docs/_includes/nav/options.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<li>
2121
<a href="#ajax">Connecting to a remote data source</a>
2222
</li>
23+
<li>
24+
<a href="#selection-api-access">Accessing a selection data</a>
25+
</li>
2326
</ul>
2427
</li>
2528
<li>

docs/_includes/options/data.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ <h1>
66
{% include options/data/select.html %}
77
{% include options/data/array.html %}
88
{% include options/data/ajax.html %}
9+
{% include options/data/selection-access.html %}
910
</section>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<section>
2+
<h2 id="selection-api-access">
3+
How to programmatically access a selection data?
4+
</h2>
5+
6+
<p>
7+
There are few ways to programmatically access the selection data. Calling <code>select2('data')</code> will return the JavaScript array of an objects representing the current selection. Each object will have properties/values which was in the source data objects passed through <code>processResults</code> and <code>templateResult</code> functions (as in <a href="#data">Loading data from an array</a> and <a href="#ajax">Connecting to a remote data source</a>).
8+
</p>
9+
10+
{% highlight js linenos %}
11+
$('select').select2('data');
12+
{% endhighlight %}
13+
14+
<p>
15+
As Select2 uses the HTML <code>&lt;SELECT&gt;</code> element to store the selection result, the selection data are represented by <code>&lt;OPTION&gt;</code> elements and can be accessed in the plain jQuery/DOM manner. The resulting elements will have properties/values from the source data objects, stored by the means of jQuery <code>data()</code> method and accessible by key <code>'data'</code>:
16+
</p>
17+
18+
{% highlight js linenos %}
19+
// Retrieve source data object's data of the first selected element
20+
$('select').find(':selected').data('data');
21+
{% endhighlight %}
22+
23+
<p>
24+
Another technique is not to rely on jQuery's <code>data()</code> method but to extend the <code>&lt;OPTION&gt;</code> elements representing selection with the HTML data-* attributes containing arbitrary data from the source data objects:
25+
</p>
26+
27+
{% highlight js linenos %}
28+
$('select').select2({
29+
// ...
30+
templateSelection: function (data, container) {
31+
$(data.element).attr('data-custom-attribute', data.customValue);
32+
return data.text;
33+
}
34+
});
35+
36+
// Retrieve custom attribute value of the first selected element
37+
$('select').find(':selected').attr('data-custom-attribute')
38+
{% endhighlight %}
39+
40+
</section>

0 commit comments

Comments
 (0)