From 8bc4f5d41dad2b34b70c5e3f30332b7c84d0a736 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 26 May 2016 20:50:12 -0400 Subject: [PATCH] Added createTag code samples This lays down part of the path for https://github.com/select2/select2/issues/4023 as well as https://github.com/select2/select2/issues/3974. --- docs/_includes/options/dropdown/tagging.html | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/docs/_includes/options/dropdown/tagging.html b/docs/_includes/options/dropdown/tagging.html index 7ab3fe8c..43e1eb01 100644 --- a/docs/_includes/options/dropdown/tagging.html +++ b/docs/_includes/options/dropdown/tagging.html @@ -19,18 +19,57 @@ $('select').select2({ Does tagging work with a single select? +

+ Yes. +

+ {% include options/not-written.html %}

How do I add extra properties to the tag?

+{% highlight js linenos %} +$('select').select2({ + createTag: function (params) { + var term = $.trim(params.term); + + if (term === '') { + return null; + } + + return { + id: term, + text: term, + newTag: true // add additional parameters + } + } +}); +{% endhighlight %} + {% include options/not-written.html %}

Can I control when tags are created?

+{% highlight js linenos %} +$('select').select2({ + createTag: function (params) { + // Don't offset to create a tag if there is no @ symbol + if (params.term.indexOf('@') === -1) { + // Return null to disable tag creation + return null; + } + + return { + id: params.term, + text: params.term + } + } +}); +{% endhighlight %} + {% include options/not-written.html %}