Skip to content

Commit 8bc4f5d

Browse files
committed
Added createTag code samples
This lays down part of the path for select2#4023 as well as select2#3974.
1 parent 7eab29e commit 8bc4f5d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/_includes/options/dropdown/tagging.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,57 @@ <h3>
1919
Does tagging work with a single select?
2020
</h3>
2121

22+
<p>
23+
Yes.
24+
</p>
25+
2226
{% include options/not-written.html %}
2327

2428
<h3>
2529
How do I add extra properties to the tag?
2630
</h3>
2731

32+
{% highlight js linenos %}
33+
$('select').select2({
34+
createTag: function (params) {
35+
var term = $.trim(params.term);
36+
37+
if (term === '') {
38+
return null;
39+
}
40+
41+
return {
42+
id: term,
43+
text: term,
44+
newTag: true // add additional parameters
45+
}
46+
}
47+
});
48+
{% endhighlight %}
49+
2850
{% include options/not-written.html %}
2951

3052
<h3>
3153
Can I control when tags are created?
3254
</h3>
3355

56+
{% highlight js linenos %}
57+
$('select').select2({
58+
createTag: function (params) {
59+
// Don't offset to create a tag if there is no @ symbol
60+
if (params.term.indexOf('@') === -1) {
61+
// Return null to disable tag creation
62+
return null;
63+
}
64+
65+
return {
66+
id: params.term,
67+
text: params.term
68+
}
69+
}
70+
});
71+
{% endhighlight %}
72+
3473
{% include options/not-written.html %}
3574

3675
<h3>

0 commit comments

Comments
 (0)