programmatic-control.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <section>
  2. <h1 id="programmatic-control" class="page-header">
  3. Programmatic control
  4. </h1>
  5. <h2 id="events">DOM events</h2>
  6. <p>
  7. Select2 will trigger some events on the original select element,
  8. allowing you to integrate it with other components. You can find more
  9. information on events
  10. <a href="options.html#events">on the options page</a>.
  11. </p>
  12. <p>
  13. <code>change</code> is fired whenever an option is selected or removed.
  14. </p>
  15. <p>
  16. <code>select2:open</code> is fired whenever the dropdown is opened.
  17. <code>select2:opening</code> is fired before this and can be prevented.
  18. </p>
  19. <p>
  20. <code>select2:close</code> is fired whenever the dropdown is closed.
  21. <code>select2:closing</code> is fired before this and can be prevented.
  22. </p>
  23. <p>
  24. <code>select2:select</code> is fired whenever a result is selected.
  25. <code>select2:selecting</code> is fired before this and can be prevented.
  26. </p>
  27. <p>
  28. <code>select2:unselect</code> is fired whenever a result is unselected.
  29. <code>select2:unselecting</code> is fired before this and can be prevented.
  30. </p>
  31. <div class="s2-example">
  32. <p>
  33. <select class="js-states js-example-events form-control"></select>
  34. </p>
  35. <p>
  36. <select class="js-states js-example-events form-control" multiple="multiple"></select>
  37. </p>
  38. </div>
  39. <div class="s2-event-log">
  40. <ul class="js-event-log"></ul>
  41. </div>
  42. <pre data-fill-from=".js-code-events"></pre>
  43. <script type="text/javascript" class="js-code-events">
  44. var $eventLog = $(".js-event-log");
  45. var $eventSelect = $(".js-example-events");
  46. $eventSelect.on("select2:open", function (e) { log("select2:open", e); });
  47. $eventSelect.on("select2:close", function (e) { log("select2:close", e); });
  48. $eventSelect.on("select2:select", function (e) { log("select2:select", e); });
  49. $eventSelect.on("select2:unselect", function (e) { log("select2:unselect", e); });
  50. $eventSelect.on("change", function (e) { log("change"); });
  51. function log (name, evt) {
  52. if (!evt) {
  53. var args = "{}";
  54. } else {
  55. var args = JSON.stringify(evt.params, function (key, value) {
  56. if (value && value.nodeName) return "[DOM node]";
  57. if (value instanceof $.Event) return "[$.Event]";
  58. return value;
  59. });
  60. }
  61. var $e = $("<li>" + name + " -> " + args + "</li>");
  62. $eventLog.append($e);
  63. $e.animate({ opacity: 1 }, 10000, 'linear', function () {
  64. $e.animate({ opacity: 0 }, 2000, 'linear', function () {
  65. $e.remove();
  66. });
  67. });
  68. }
  69. </script>
  70. <h2 id="programmatic">Programmatic access</h2>
  71. <p>
  72. Select2 supports methods that allow programmatic control of the
  73. component.
  74. </p>
  75. <div class="s2-example">
  76. <p>
  77. <select class="js-example-programmatic js-states form-control"></select>
  78. </p>
  79. <div class="btn-toolbar" role="toolbar" aria-label="Programmatic control">
  80. <div class="btn-group btn-group-sm" aria-label="Set Select2 option">
  81. <button class="js-programmatic-set-val btn btn-default">
  82. Set "California"
  83. </button>
  84. </div>
  85. <div class="btn-group btn-group-sm" role="group" aria-label="Open and close">
  86. <button class="js-programmatic-open btn btn-default">
  87. Open
  88. </button>
  89. <button class="js-programmatic-close btn btn-default">
  90. Close
  91. </button>
  92. </div>
  93. <div class="btn-group btn-group-sm" role="group" aria-label="Initialize and destroy">
  94. <button class="js-programmatic-init btn btn-default">
  95. Init
  96. </button>
  97. <button class="js-programmatic-destroy btn btn-default">
  98. Destroy
  99. </button>
  100. </div>
  101. </div>
  102. <p>
  103. <select class="js-example-programmatic-multi js-states form-control" multiple="multiple"></select>
  104. </p>
  105. <div class="btn-group btn-group-sm" role="group" aria-label="Programmatic setting and clearing Select2 options">
  106. <button type="button" class="js-programmatic-multi-set-val btn btn-default">
  107. Set to California and Alabama
  108. </button>
  109. <button type="button" class="js-programmatic-multi-clear btn btn-default">
  110. Clear
  111. </button>
  112. </div>
  113. </div>
  114. <pre data-fill-from=".js-code-programmatic"></pre>
  115. <script type="text/javascript" class="js-code-programmatic">
  116. var $example = $(".js-example-programmatic").select2();
  117. var $exampleMulti = $(".js-example-programmatic-multi").select2();
  118. $(".js-programmatic-set-val").on("click", function () { $example.val("CA").trigger("change"); });
  119. $(".js-programmatic-open").on("click", function () { $example.select2("open"); });
  120. $(".js-programmatic-close").on("click", function () { $example.select2("close"); });
  121. $(".js-programmatic-init").on("click", function () { $example.select2(); });
  122. $(".js-programmatic-destroy").on("click", function () { $example.select2("destroy"); });
  123. $(".js-programmatic-multi-set-val").on("click", function () { $exampleMulti.val(["CA", "AL"]).trigger("change"); });
  124. $(".js-programmatic-multi-clear").on("click", function () { $exampleMulti.val(null).trigger("change"); });
  125. </script>
  126. </section>