array.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <section>
  2. <h2 id="data">
  3. Can I load data into Select2 using an array?
  4. </h2>
  5. <p>
  6. While Select2 is designed to be used with a <code>&lt;select&gt;</code> tag
  7. the data that is used to search through and display the results can be
  8. loaded from a JavaScript array using the <code>data</code> option. This
  9. option should be passed in during the initialization of Select2.
  10. </p>
  11. {% highlight js linenos %}
  12. $('select').select2({
  13. data: [
  14. {
  15. id: 'value',
  16. text: 'Text to display'
  17. },
  18. // ... more data objects ...
  19. ]
  20. });
  21. {% endhighlight %}
  22. <h3>
  23. What properties are required on the objects passed in to the array?
  24. </h3>
  25. <p>
  26. The <code>id</code> and <code>text</code> properties are required on each
  27. object, and these are the properties that Select2 uses for the internal
  28. data objects. Any additional paramters passed in with data objects will be
  29. included on the data objects that Select2 exposes.
  30. </p>
  31. <h3>
  32. How should nested results be formatted?
  33. </h3>
  34. <p>
  35. Nested results should be specified using the <code>children</code> property
  36. on the data objects that are passed in. This <code>children</code> property
  37. should be an array of data objects that are grouped under this option, and
  38. the label for the group should be specified as the <code>text</code>
  39. property on the root data object.
  40. </p>
  41. {% highlight js linenos %}
  42. {
  43. text: 'Group label',
  44. children: [
  45. {
  46. id: 'nested-1',
  47. text: 'First nested option'
  48. },
  49. // ... more data objects ...
  50. ]
  51. }
  52. {% endhighlight %}
  53. <h3>
  54. How many levels of nesting are allowed?
  55. </h3>
  56. <p>
  57. Because Select2 falls back to an <code>&lt;optgroup&gt;</code> when
  58. creating nested options, only
  59. <a href="#how-many-levels-of-nesting-can-there-be">a single level of nesting</a>
  60. is supported. Any additional levels of nesting is not guarenteed to be
  61. displayed properly across all browsers and devices.
  62. </p>
  63. <h3>
  64. Why are <code>&lt;option&gt;</code> tags being created?
  65. </h3>
  66. <p>
  67. The <code>data</code> option is a shortcut that Select2 provides which
  68. allows you to load options into your <code>select</code> from a data array.
  69. </p>
  70. {% include options/not-written.html %}
  71. <h3>
  72. My objects don&apos;t use <code>id</code> for their unique identifiers,
  73. what can I do?
  74. </h3>
  75. <p>
  76. Select2 requires that the <code>id</code> property is used to uniquely
  77. identify the options that are displayed in the results list. If you use a
  78. property other than <code>id</code> (like <code>pk</code>) to uniquely
  79. identify an option, you need to map your old property to <code>id</code>
  80. before passing it to Select2.
  81. </p>
  82. <p>
  83. If you cannot do this on your server or you are in a situation where the
  84. identifier cannot be changed, you can do this in JavaScript before passing
  85. it to Select2.
  86. </p>
  87. {% highlight js linenos %}
  88. var data = $.map(yourArrayData, function (obj) {
  89. obj.id = obj.id || obj.pk; // replace pk with your identifier
  90. return obj;
  91. });
  92. {% endhighlight %}
  93. <h3>
  94. My objects use a property other than <code>text</code> for the text that
  95. needs to be displayed
  96. </h3>
  97. <p>
  98. Just like with the <code>id</code> property, Select2 requires that the text
  99. that should be displayed for an option is stored in the <code>text</code>
  100. property. You can map this property from any existing property using the
  101. following JavaScript.
  102. </p>
  103. {% highlight js linenos %}
  104. var data = $.map(yourArrayData, function (obj) {
  105. obj.text = obj.text || obj.name; // replace name with the property used for the text
  106. return obj;
  107. });
  108. {% endhighlight %}
  109. </section>