themes-templating-responsive-design.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <section>
  2. <h1 id="themes-templating-responsive-design" class="page-header">
  3. Themes, templating and responsive design
  4. </h1>
  5. <h2 id="themes">Theme support</h2>
  6. <p>
  7. Select2 supports custom themes using the
  8. <a href="options.html#theme">theme option</a>
  9. so you can style Select2 to match the rest of your application.
  10. </p>
  11. <p>
  12. These are using the <code>classic</code> theme, which matches the old
  13. look of Select2.
  14. </p>
  15. <div class="s2-example">
  16. <p>
  17. <select class="js-example-theme-single js-states form-control">
  18. </select>
  19. </p>
  20. <p>
  21. <select class="js-example-theme-multiple js-states form-control" multiple="multiple"></select>
  22. </p>
  23. </div>
  24. <pre data-fill-from=".js-code-theme"></pre>
  25. <script type="text/x-example-code" class="js-code-theme">
  26. $(".js-example-theme-single").select2({
  27. theme: "classic"
  28. });
  29. $(".js-example-theme-multiple").select2({
  30. theme: "classic"
  31. });
  32. </script>
  33. <h2 id="templating">Templating</h2>
  34. <p>
  35. Various display options of the Select2 component can be changed:
  36. You can access the <code>&lt;option&gt;</code> element
  37. (or <code>&lt;optgroup&gt;</code>) and any attributes on those elements
  38. using <code>.element</code>.
  39. </p>
  40. <p>
  41. Templating is primarily controlled by the
  42. <a href="options.html#templateResult"><code>templateResult</code></a>
  43. and <a href="options.html#templateSelection"><code>templateSelection</code></a>
  44. options.
  45. </p>
  46. <div class="s2-example">
  47. <p>
  48. <select class="js-example-templating js-states form-control"></select>
  49. </p>
  50. </div>
  51. <pre data-fill-from=".js-code-templating"></pre>
  52. <script type="text/x-example-code" class="js-code-templating">
  53. function formatState (state) {
  54. if (!state.id) { return state.text; }
  55. var $state = $(
  56. '<span><img src="vendor/images/flags/' + state.element.value.toLowerCase() + '.png" class="img-flag" /> ' + state.text + '</span>'
  57. );
  58. return $state;
  59. };
  60. $(".js-example-templating").select2({
  61. templateResult: formatState
  62. });
  63. </script>
  64. <h2 id="responsive">Responsive design - Percent width</h2>
  65. <p>
  66. Select2's width can be set to a percentage of its parent to support
  67. responsive design. The two Select2 boxes below are styled to 50% and 75%
  68. width respectively.
  69. </p>
  70. <div class="s2-example">
  71. <p>
  72. <select class="js-example-responsive js-states" style="width: 50%"></select>
  73. </p>
  74. <p>
  75. <select class="js-example-responsive js-states" multiple="multiple" style="width: 75%"></select>
  76. </p>
  77. </div>
  78. <pre data-fill-from=".js-code-responsive"></pre>
  79. <div class="alert alert-warning">
  80. Select2 will do its best to resolve the percent width specified via a
  81. css class, but it is not always possible. The best way to ensure that
  82. Select2 is using a percent based width is to inline the
  83. <code>style</code> declaration into the tag.
  84. </div>
  85. <script type="text/x-example-code" class="js-code-responsive">
  86. <select class="js-example-responsive" style="width: 50%"></select>
  87. <select class="js-example-responsive" multiple="multiple" style="width: 75%"></select>
  88. </script>
  89. </section>