options.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <section>
  2. <h2 id="options">
  3. How should Select2 be initialized?
  4. </h2>
  5. <h3 id="setting-default-options">
  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>.
  24. </p>
  25. <pre class="prettyprint">
  26. $.fn.select2.defaults.set("theme", "classic");
  27. </pre>
  28. <h3>
  29. How can I set a default value for a nested option?
  30. </h3>
  31. <p>
  32. The key that is
  33. set should take the same format as keys set using
  34. <a href="#data-attributes">HTML <code>data-*</code> attributes</a> which
  35. means that two dashes (<code>--</code>) will be replaced by a level of
  36. nesting, and a single dash (<code>-</code>) will convert it to a camelCase
  37. string.
  38. </p>
  39. <h3>
  40. How can I reset all of the global default options?
  41. </h3>
  42. <p>
  43. You can reset the default options by calling
  44. </p>
  45. <pre class="prettyprint">
  46. $.fn.select2.defaults.reset();
  47. </pre>
  48. </section>