瀏覽代碼

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.
Kevin Brown 9 年之前
父節點
當前提交
8bc4f5d41d
共有 1 個文件被更改,包括 39 次插入0 次删除
  1. 39 0
      docs/_includes/options/dropdown/tagging.html

+ 39 - 0
docs/_includes/options/dropdown/tagging.html

@@ -19,18 +19,57 @@ $('select').select2({
     Does tagging work with a single select?
   </h3>
 
+  <p>
+    Yes.
+  </p>
+
   {% include options/not-written.html %}
 
   <h3>
     How do I add extra properties to the tag?
   </h3>
 
+{% 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 %}
 
   <h3>
     Can I control when tags are created?
   </h3>
 
+{% 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 %}
 
   <h3>