Quellcode durchsuchen

Answer question about using HTML in selection template

This closes https://github.com/select2/select2/pull/4606.
Stephen A. Wilson vor 8 Jahren
Ursprung
Commit
0a67287c7a
1 geänderte Dateien mit 15 neuen und 2 gelöschten Zeilen
  1. 15 2
      docs/_includes/options/selections/templating.html

+ 15 - 2
docs/_includes/options/selections/templating.html

@@ -27,11 +27,24 @@ $('select').select2({
     I am using HTML in my selection template but it isn't displaying it
   </h3>
 
-  {% include options/not-written.html %}
+  <p>
+    If you want to use HTML in your selection template, you will need to return a jQuery object. Otherwise, Select2 will assume that your template only returns text and will escape it.
+  </p>
+
+{% highlight js linenos %}
+function template(data, container) {
+  return $('<strong></strong>')
+    .text(data.text);
+}
+
+$('select').select2({
+  templateSelection: template
+});
+{% endhighlight %}
 
   <h3>
     How can I access the container where the selection is displayed?
   </h3>
 
   {% include options/not-written.html %}
-</section>
+</section>