themes-templating-responsive-design.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. {% highlight js linenos %}
  25. $(".js-example-theme-single").select2({
  26. theme: "classic"
  27. });
  28. $(".js-example-theme-multiple").select2({
  29. theme: "classic"
  30. });
  31. {% endhighlight %}
  32. <h2 id="templating">Templating</h2>
  33. <p>
  34. Various display options of the Select2 component can be changed:
  35. You can access the <code>&lt;option&gt;</code> element
  36. (or <code>&lt;optgroup&gt;</code>) and any attributes on those elements
  37. using <code>.element</code>.
  38. </p>
  39. <p>
  40. Templating is primarily controlled by the
  41. <a href="options.html#templateResult"><code>templateResult</code></a>
  42. and <a href="options.html#templateSelection"><code>templateSelection</code></a>
  43. options.
  44. </p>
  45. <div class="s2-example">
  46. <p>
  47. <select class="js-example-templating js-states form-control"></select>
  48. </p>
  49. </div>
  50. {% highlight js linenos %}
  51. function formatState (state) {
  52. if (!state.id) { return state.text; }
  53. var $state = $(
  54. '<span><img src="vendor/images/flags/' + state.element.value.toLowerCase() + '.png" class="img-flag" /> ' + state.text + '</span>'
  55. );
  56. return $state;
  57. };
  58. $(".js-example-templating").select2({
  59. templateResult: formatState
  60. });
  61. {% endhighlight %}
  62. <h2 id="responsive">Responsive design - Percent width</h2>
  63. <p>
  64. Select2's width can be set to a percentage of its parent to support
  65. responsive design. The two Select2 boxes below are styled to 50% and 75%
  66. width respectively.
  67. </p>
  68. <div class="s2-example">
  69. <p>
  70. <select class="js-example-responsive js-states" style="width: 50%"></select>
  71. </p>
  72. <p>
  73. <select class="js-example-responsive js-states" multiple="multiple" style="width: 75%"></select>
  74. </p>
  75. </div>
  76. {% highlight html linenos %}
  77. <select class="js-example-responsive" style="width: 50%"></select>
  78. <select class="js-example-responsive" multiple="multiple" style="width: 75%"></select>
  79. {% endhighlight %}
  80. <div class="alert alert-warning">
  81. Select2 will do its best to resolve the percent width specified via a
  82. css class, but it is not always possible. The best way to ensure that
  83. Select2 is using a percent based width is to inline the
  84. <code>style</code> declaration into the tag.
  85. </div>
  86. </section>