|
| 1 | +--- |
| 2 | +group: javascript-developer-guide |
| 3 | +title: Multiselect widget |
| 4 | +contributor_name: Atwix |
| 5 | +contributor_link: https://www.atwix.com/ |
| 6 | +--- |
| 7 | + |
| 8 | +This [widget](https://glossary.magento.com/widget/) enables multiple select capability in the search field to help users choose different options. |
| 9 | + |
| 10 | +The Multiselect widget source is the [lib/web/mage/multiselect.js][] file. |
| 11 | + |
| 12 | +## Initialize the Multiselect widget |
| 13 | + |
| 14 | +For information about how to initialize a widget in a JS component or `.phtml` template, see the [Initialize JavaScript][] topic. |
| 15 | + |
| 16 | +The following example shows how to instantiate the Multiselect widget: |
| 17 | + |
| 18 | +```javascript |
| 19 | +$("#multiselect").multiselect2({}); |
| 20 | +``` |
| 21 | + |
| 22 | +Where: |
| 23 | + |
| 24 | +- `#multiselect` is the selector of the select element for which Multiselect is initialized. |
| 25 | + |
| 26 | +Phtml template file examples using script: |
| 27 | + |
| 28 | +```html |
| 29 | +<script> |
| 30 | + require([ |
| 31 | + 'jquery', |
| 32 | + 'mage/multiselect' |
| 33 | + ], function ($) { |
| 34 | + 'use strict'; |
| 35 | +
|
| 36 | + $("#multiselect").multiselect2({}); |
| 37 | + }); |
| 38 | +</script> |
| 39 | +``` |
| 40 | + |
| 41 | +## Options |
| 42 | + |
| 43 | +The RedirectUrl widget has the following options: |
| 44 | + |
| 45 | +- [mselectContainer](#mselectcontainer) |
| 46 | +- [mselectItemsWrapperClass](#mselectitemswrapperclass) |
| 47 | +- [mselectCheckedClass](#mselectcheckedclass) |
| 48 | +- [containerClass](#containerclass) |
| 49 | +- [searchInputClass](#searchinputclass) |
| 50 | +- [selectedItemsCountClass](#selecteditemscountclass) |
| 51 | +- [currentPage](#currentpage) |
| 52 | +- [lastAppendValue](#lastappendvalue) |
| 53 | +- [updateDelay](#updatedelay) |
| 54 | + |
| 55 | +### `mselectContainer` |
| 56 | + |
| 57 | +Multiselect container selector. |
| 58 | + |
| 59 | +**Type**: String |
| 60 | + |
| 61 | +**Default value**: `'section.mselect-list'` |
| 62 | + |
| 63 | +### `mselectItemsWrapperClass` |
| 64 | + |
| 65 | +Multiselect items wrapper class. |
| 66 | + |
| 67 | +**Type**: String |
| 68 | + |
| 69 | +**Default value**: `'mselect-items-wrapper'` |
| 70 | + |
| 71 | +### `mselectCheckedClass` |
| 72 | + |
| 73 | +The class which is attached to a checked multi-select item. |
| 74 | + |
| 75 | +**Type**: String |
| 76 | + |
| 77 | +**Default value**: `'mselect-checked'` |
| 78 | + |
| 79 | +### `containerClass` |
| 80 | + |
| 81 | +The class which is attached to the container with [multi-select container selector](#mselectcontainer). |
| 82 | + |
| 83 | +**Type**: String |
| 84 | + |
| 85 | +**Default value**: `'paginated'` |
| 86 | + |
| 87 | +### `searchInputClass` |
| 88 | + |
| 89 | +Class of the search input. |
| 90 | + |
| 91 | +**Type**: String |
| 92 | + |
| 93 | +**Default value**: `'admin__action-multiselect-search'` |
| 94 | + |
| 95 | +### `selectedItemsCountClass` |
| 96 | + |
| 97 | +Class of the selected items counter. |
| 98 | + |
| 99 | +**Type**: String |
| 100 | + |
| 101 | +**Default value**: `'admin__action-multiselect-search'` |
| 102 | + |
| 103 | +### `currentPage` |
| 104 | + |
| 105 | +Current page of multi-select items. |
| 106 | + |
| 107 | +**Type**: Integer |
| 108 | + |
| 109 | +**Default value**: `1` |
| 110 | + |
| 111 | +### `lastAppendValue` |
| 112 | + |
| 113 | +The value of the last added multi-select item. |
| 114 | + |
| 115 | +**Type**: Integer, String |
| 116 | + |
| 117 | +**Default value**: `0` |
| 118 | + |
| 119 | +### `updateDelay` |
| 120 | + |
| 121 | +The search field update delay. |
| 122 | + |
| 123 | +**Type**: Integer, String |
| 124 | + |
| 125 | +**Default value**: `0` |
| 126 | + |
| 127 | +## Code sample |
| 128 | + |
| 129 | +The following example shows the multiselect field with search bar and **Add new value** button. |
| 130 | + |
| 131 | +```html |
| 132 | +<div class="select-example"> |
| 133 | + <select id="multiselect" name="multiselect-field" multiple="multiple"> |
| 134 | + <option value="1">Option 1</option> |
| 135 | + <option value="2">Option 2</option> |
| 136 | + <option value="3">Option 3</option> |
| 137 | + <option value="4">Option 4</option> |
| 138 | + <option value="5">Option 5</option> |
| 139 | + <option value="6">Option 6</option> |
| 140 | + </select> |
| 141 | + <script> |
| 142 | + require(['jquery', 'mage/multiselect'], function ($) { |
| 143 | + $('#multiselect').multiselect2({selectedValues: [2, 4]}); |
| 144 | + }); |
| 145 | + </script> |
| 146 | +</div> |
| 147 | +``` |
| 148 | + |
| 149 | +## Result |
| 150 | + |
| 151 | +As a result, we see the multiselect field with search bar and **Add new value** button. |
| 152 | +You can now search for an option in the search bar and the **Add new value** button allows you to add a new option to the multiselect feature. |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +<!-- Link Definitions --> |
| 157 | +[lib/web/mage/multiselect.js]: {{ site.mage2bloburl }}/{{ page.guide_version }}/lib/web/mage/multiselect.js |
| 158 | +[Initialize JavaScript]: {{page.baseurl}}/javascript-dev-guide/javascript/js_init.html |
0 commit comments