options.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <section>
  2. <h2 id="options">
  3. How should Select2 be initialized?
  4. </h2>
  5. <h3>
  6. Can default options be set for all dropdowns?
  7. </h3>
  8. <p>
  9. In some cases, you need to set the default options for all instances of
  10. Select2 in your web application. This is especially useful when you are
  11. migrating from past versions of Select2, or you are using non-standard
  12. options <a href="#amd">like custom AMD builds</a>. Select2 exposes the
  13. default options through <code>$.fn.select2.defaults</code>, which allows
  14. you to set them globally.
  15. </p>
  16. <p>
  17. When setting options globally, any past defaults that have been set will
  18. be overriden. Default options are only used when an option is requested
  19. that has not been set during initialization.
  20. </p>
  21. <p>
  22. <strong>You can set default options</strong> by calling
  23. <code>$.fn.select2.defaults.set("key", "value")</code>. The key that is
  24. set should take the same format as keys set using
  25. <a href="#data-attributes">HTML <code>data-*</code> attributes</a> which
  26. means that two dashes (<code>--</code>) will be replaced by a level of
  27. nesting, and a single dash (<code>-</code>) will convert it to a camelCase
  28. string.
  29. </p>
  30. <pre class="prettyprint">
  31. $.fn.select2.defaults.set("theme", "classic");
  32. </pre>
  33. <h3>
  34. How can I set a default value for a nested option?
  35. </h3>
  36. <h3>
  37. How can I reset all of the global default options?
  38. </h3>
  39. <p>
  40. You can reset the default options by calling
  41. </p>
  42. <pre class="prettyprint">
  43. $.fn.select2.defaults.reset();
  44. </pre>
  45. </section>