select.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <section>
  2. <h2 id="data-adapters-select-tag">
  3. Can Select2 be used with a <code>&lt;select&gt;</code> tag?
  4. </h2>
  5. <p>
  6. Select2 was designed to be a replacement for the standard <code>&lt;select&gt;</code> boxes that are displayed by the browser, so by default it supports all options and operations that are available in a standard select box, but with added flexibility. There is no special configuration required to make Select2 work with a <code>&lt;select&gt;</code> tag.
  7. </p>
  8. <h3>
  9. Does Select2 support nesting options?
  10. </h3>
  11. <p>
  12. A standard <code>&lt;select&gt;</code> box can display nested options by wrapping them with in an <code>&lt;optgroup&gt;</code> tag.
  13. </p>
  14. {% highlight html linenos %}
  15. <select>
  16. <optgroup label="Group Name">
  17. <option>Nested option</option>
  18. </optgroup>
  19. </select>
  20. {% endhighlight %}
  21. <h3>
  22. How many levels of nesting can there be?
  23. </h3>
  24. <p>
  25. Only a single level of nesting is allowed per the HTML specification. If you nest an <code>&lt;optgroup&gt;</code> within another <code>&lt;optgroup&gt;</code>, Select2 will not be able to detect the extra level of nesting and errors may be triggered as a result.
  26. </p>
  27. <h3>
  28. Can <code>&lt;optgroup&gt;</code> tags be made selectable?
  29. </h3>
  30. <p>
  31. No. This is a limitation of the HTML specification and is not a limitation that Select2 can overcome. You can emulate grouping by using an <code>&lt;option&gt;</code> instead of an <code>&lt;optgroup&gt;</code> and <a href="http://stackoverflow.com/q/30820215/359284#30948247">changing the style by using CSS</a>, but this is not recommended as it is not fully accessible.
  32. </p>
  33. <h3>
  34. How are <code>&lt;option&gt;</code> and <code>&lt;optgroup&gt;</code> tags serialized into data objects?
  35. </h3>
  36. <p>
  37. Select2 will convert the <code>&lt;option&gt;</code> tag into a data object based on the following rules.
  38. </p>
  39. {% highlight js linenos %}
  40. {
  41. "id": "value attribute" || "option text",
  42. "text": "label attribute" || "option text",
  43. "element": HTMLOptionElement
  44. }
  45. {% endhighlight %}
  46. <p>
  47. And <code>&lt;optgroup&gt;</code> tags will be converted into data objects using the following rules
  48. </p>
  49. {% highlight js linenos %}
  50. {
  51. "text": "label attribute",
  52. "children": [ option data object, ... ],
  53. "elment": HTMLOptGroupElement
  54. }
  55. {% endhighlight %}
  56. </section>