settings-default-options.html 1.4 KB

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