adapters.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <section>
  2. <div class="page-header">
  3. <h1 id="adapters">Adapters</h1>
  4. </div>
  5. <p>
  6. Select2 allows plugins to add additional functionality through the core
  7. adapters. You can change almost anything involving the way Select2 works
  8. to the way Select2 interacts with the page by modifying the core adapters.
  9. Most third-party plugins should provide decorators (used to wrap adapters)
  10. and custom adapters that you can use.
  11. </p>
  12. <p>
  13. Each adapter contains a set of methods which will must always be defined.
  14. Along with the global methods that all adapters must implement, these
  15. methods must be implemented.
  16. </p>
  17. <h2 id="adapters-all">
  18. All adapters
  19. </h2>
  20. <p>
  21. All adapters must implement a set of methods that Select2 will use to
  22. display them and bind any internal events.
  23. </p>
  24. <pre class="prettyprint linenums">
  25. // The basic HTML that should be rendered by Select2. A jQuery or DOM element
  26. // should be returned, which will automatically be placed by Select2 within the
  27. // DOM.
  28. //
  29. // @returns A jQuery or DOM element that contains any elements that must be
  30. // rendered by Select2.
  31. Adapter.render = function () {
  32. return $jq;
  33. };
  34. // Bind to any Select2 or DOM events.
  35. //
  36. // @param container The Select2 object that is bound to the jQuery element. You
  37. // can listen to Select2 events with `on` and trigger Select2 events using the
  38. // `trigger` method.
  39. // @param $container The jQuery DOM node that all default adapters will be
  40. // rendered within.
  41. Adapter.bind = function (container, $container) { };
  42. // Position the DOM element within the Select2 DOM container, or in another
  43. // place. This allows adapters to be located outside of the Select2 DOM,
  44. // such as at the end of the document or in a specific place within the Select2
  45. // DOM node.
  46. //
  47. // Note: This method is not called on data adapters.
  48. //
  49. // @param $rendered The rendered DOM element that was returned from the call to
  50. // `render`. This may have been modified by Select2, but the root element
  51. // will always be the same.
  52. // @param $defaultContainer The default container that Select2 will typically
  53. // place the rendered DOM element within. For most adapters, this is the
  54. // Select2 DOM element.
  55. Adapter.position = function ($rendered, $defaultContainer) { };
  56. // Destroy any events or DOM elements that have been created.
  57. // This is called when `select2("destroy")` is called on an element.
  58. Adapter.destroy = function () { };
  59. </pre>
  60. <h2 id="selectionAdapter">
  61. Container (selection)
  62. </h2>
  63. <p>
  64. The selection is what is shown to the user as a replacement of the
  65. standard <code>&lt;select&gt;</code> box. It controls the display of the
  66. selection option(s), as well anything else that needs to be embedded
  67. within the container, such as a search box.
  68. </p>
  69. <dl class="dl-horizontal">
  70. <dt>Key</dt>
  71. <dd>
  72. <code>selectionAdapter</code>
  73. </dd>
  74. <dt>Default</dt>
  75. <dd>
  76. <code title="select2/selection/single">SingleSelection</code> or
  77. <code title="select2/selection/multiple">MultipleSelection</code>
  78. </dd>
  79. <dt>Base</dt>
  80. <dd>
  81. <code title="select2/selection/base">BaseSelection</code>
  82. </dd>
  83. </dl>
  84. <pre class="prettyprint linenums">
  85. // Update the selected data.
  86. //
  87. // @param data An array of data objects that have been generated by the data
  88. // adapter. If no objects should be selected, an empty array will be passed.
  89. //
  90. // Note: An array will always be passed into this method, even if Select2 is
  91. // attached to a source which only accepts a single selection.
  92. SelectionAdapter.update = function (data) { };
  93. </pre>
  94. <h2 id="dataAdapter">
  95. Data set
  96. </h2>
  97. <p>
  98. The data set is what Select2 uses to generate the possible results that
  99. can be selected, as well as the currently selected results.
  100. </p>
  101. <dl class="dl-horizontal">
  102. <dt>Key</dt>
  103. <dd>
  104. <code>dataAdapter</code>
  105. </dd>
  106. <dt>Default</dt>
  107. <dd>
  108. <code title="select2/data/select">SelectAdapter</code>
  109. </dd>
  110. <dt>Base</dt>
  111. <dd>
  112. <code title="select2/data/base">BaseAdapter</code>
  113. </dd>
  114. </dl>
  115. <pre class="prettyprint linenums">
  116. // Get the currently selected options. This is called when trying to get the
  117. // initial selection for Select2, as well as when Select2 needs to determine
  118. // what options within the results are selected.
  119. //
  120. // @param callback A function that should be called when the current selection
  121. // has been retrieved. The first parameter to the function should be an array
  122. // of data objects.
  123. DataAdapter.current = function (callback) {
  124. callback(currentData);
  125. }
  126. // Get a set of options that are filtered based on the parameters that have
  127. // been passed on in.
  128. //
  129. // @param params An object containing any number of parameters that the query
  130. // could be affected by. Only the core parameters will be documented.
  131. // @param params.term A user-supplied term. This is typically the value of the
  132. // search box, if one exists, but can also be an empty string or null value.
  133. // @param params.page The specific page that should be loaded. This is typically
  134. // provided when working with remote data sets, which rely on pagination to
  135. // determine what objects should be displayed.
  136. // @param callback The function that should be called with the queried results.
  137. DataAdapter.query = function (params, callback) {
  138. callback(queryiedData);
  139. }
  140. </pre>
  141. <h2 id="dropdownAdapter">
  142. Dropdown
  143. </h2>
  144. <p>
  145. The dropdown adapter defines the main container that the dropdown should
  146. be held in. <strong>It does not define any extra methods that can be used
  147. for decorators</strong>, but it is common for decorators to attach to the
  148. <code>render</code> and <code>position</code> methods to alter how the
  149. dropdown is altered and positioned.
  150. </p>
  151. <dl class="dl-horizontal">
  152. <dt>Key</dt>
  153. <dd>
  154. <code>dropdownAdapter</code>
  155. </dd>
  156. <dt>Default</dt>
  157. <dd>
  158. <code title="select2/dropdown">DropdownAdapter</code>
  159. </dd>
  160. </dl>
  161. <h2 id="resultsAdapter">
  162. Results
  163. </h2>
  164. <p>
  165. The results adapter controls the list of results that the user can select
  166. from. While the results adapter does not define any additional methods
  167. that must be implemented, it makes extensive use of the Select2 event
  168. system for controlling the display of results and messages.
  169. </p>
  170. <dl class="dl-horizontal">
  171. <dt>Key</dt>
  172. <dd>
  173. <code>resultsAdapter</code>
  174. </dd>
  175. <dt>Default</dt>
  176. <dd>
  177. <code title="select2/results">ResultsAdapter</code>
  178. </dd>
  179. </dl>
  180. </section>