### Install @vueform/multiselect
Source: https://github.com/vueform/multiselect/blob/main/README.md
Installs the @vueform/multiselect package using npm. This is the initial step to integrate the component into your Vue.js project.
```bash
npm install @vueform/multiselect
```
--------------------------------
### Vue 2 Multiselect Component Setup
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates how to set up and use the Multiselect component in a Vue 2 application. It includes importing the component, registering it, and providing sample data for model binding and options. Ensure you have the correct version of the Multiselect library installed for Vue 2.
```vue
```
--------------------------------
### Vue Multiselect: Create Option On Keypress Example
Source: https://github.com/vueform/multiselect/blob/main/README.md
Configures which keys trigger the creation of a new option when 'createOption' is enabled. This example shows creating options on 'enter' or 'space'.
```html
```
--------------------------------
### Basic Vue 3 Usage with @vueform/multiselect
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates a basic setup for using the Multiselect component in a Vue 3 application. It includes importing the component, defining data properties for model and options, and applying default styling.
```vue
```
--------------------------------
### Load Async Options from API on Open with Infinite Scroll in Vue Multiselect
Source: https://github.com/vueform/multiselect/blob/main/README.md
This example demonstrates loading options from an API asynchronously when the dropdown is first opened in Vue Multiselect. It utilizes `infinite: true` for virtualizing the option list, enabling efficient handling of large datasets. Options are not loaded until the user interacts with the dropdown, improving initial load performance.
```vue
{
if (select$.noOptions) {
select$.resolveOptions()
}
}
/>
```
--------------------------------
### Accessing Multiselect Component Instance via Ref
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates how to get a reference to the Vue Multiselect component instance using 'ref' in a template. This reference is crucial for calling its API methods.
```html
```
--------------------------------
### Vue Multiselect: Object Value Storage Example
Source: https://github.com/vueform/multiselect/blob/main/README.md
Illustrates how to store selected values as objects instead of simple strings. When 'object' is true, 'v-model' will hold objects with 'value' and 'label' properties.
```html
```
```javascript
data: {
value: [{value: 'js', label: 'Javascript'}],
options: [
{value: 'js', label: 'Javascript'},
{value: 'jsx', label: 'JSX'},
{value: 'ts', label: 'Typescript'}
]
}
```
--------------------------------
### Vue Multiselect Tags with Custom Tag Slot and Styling
Source: https://github.com/vueform/multiselect/blob/main/README.md
Shows how to use Vue-Multiselect in 'tags' mode for tag-based input, featuring a custom slot for rendering each tag. This example includes user images within the tags and custom CSS for styling the tags, demonstrating advanced UI customization. Dependencies include the Multiselect component and associated Vue template/script structure.
```vue
{{ option.name }}
```
--------------------------------
### Manage Created Tags Asynchronously in Vue Multiselect
Source: https://github.com/vueform/multiselect/blob/main/README.md
This example shows how to manage tag creation asynchronously in Vue Multiselect. It uses the `onCreate` callback to validate or modify newly created tags. The `regex` prop can restrict tag input, and the `onCreate` method can return `false` to prevent a tag from being added, or modify the tag's properties before it's accepted.
```vue
```
--------------------------------
### Vue Multiselect: Append New Option Example
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates how to automatically append a new option to the list when 'searchable' and 'createTag' (or 'createOption') are enabled. If 'appendNewOption' is false, manual appending via the '@option' event is required.
```html
```
--------------------------------
### Vue Multiselect with Localized Texts
Source: https://github.com/vueform/multiselect/blob/main/README.md
This example showcases how to implement localized texts within the Vue Multiselect component. It uses the `locale` and `fallback-locale` props to specify the desired language. Options can have labels defined for different locales, allowing the component to display the appropriate text based on the user's language settings.
```vue
```
--------------------------------
### Styling Multiselect Instance with CSS Classes
Source: https://github.com/vueform/multiselect/blob/main/README.md
This example shows how to apply custom styles to individual instances of the Vueform Multiselect component using CSS classes. By defining specific CSS variables within a class, you can target and modify the appearance of a particular multiselect instance without affecting others. This allows for granular control over styling elements like tags.
```vue
```
```css
.multiselect-green {
--ms-tag-bg: #D1FAE5;
--ms-tag-color: #059669;
}
.multiselect-blue {
--ms-tag-bg: #DBEAFE;
--ms-tag-color: #2563EB;
}
```
--------------------------------
### Set Default Values with `allowAbsent: true` in Vue Multiselect
Source: https://github.com/vueform/multiselect/blob/main/README.md
This example demonstrates using `allowAbsent: true` in Vue Multiselect when the options list is an array of strings. This prop permits values that are not explicitly listed in the options. It's useful because plain string values for options automatically use the string itself as both the label and the value.
```vue
```
--------------------------------
### Configuring Tailwind CSS for Multiselect
Source: https://github.com/vueform/multiselect/blob/main/README.md
This JavaScript snippet shows how to configure `tailwind.config.js` to use custom background images for the Vueform Multiselect component, leveraging Tailwind CSS. It requires the `mini-svg-data-uri` package to convert SVG icons into data URIs for use as background images. This setup allows for styling elements like the caret, spinner, and remove icon using Tailwind's utility classes.
```js
// tailwind.config.js
const svgToDataUri = require('mini-svg-data-uri')
module.exports = {
theme: {
extend: {
backgroundImage: (theme) => ({
'multiselect-caret': `url("${svgToDataUri(
` `,
)}")`,
'multiselect-spinner': `url("${svgToDataUri(
` `,
)}")`,
'multiselect-remove': `url("${svgToDataUri(
` `,
)}")`,
})
},
}
}
```
--------------------------------
### Opening Multiselect Options List Programmatically
Source: https://github.com/vueform/multiselect/blob/main/README.md
Shows how to use the 'open' API method on the Multiselect component instance obtained via ref. This is typically done within lifecycle hooks like 'mounted'.
```js
mounted() {
this.$refs.multiselect.open()
}
```
--------------------------------
### Setting Multiselect Mode and Searchability
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates how to configure the selection mode (single, multiple, tags) and enable or disable the search functionality for options.
```vue
```
--------------------------------
### API Methods
Source: https://github.com/vueform/multiselect/blob/main/README.md
Programmatic methods to interact with the Multiselect component instance.
```APIDOC
## API Methods
To access the API, use a `ref` on the `Multiselect` component in your template:
```html
```
Then, you can call these methods from your script:
```javascript
// Example: Opening the options list
mounted() {
this.$refs.multiselect.open();
}
```
### Methods
- **open()**: Opens the options list.
- **close()**: Closes the options list.
- **select(option)**: Selects an option based on its value. `option` is the value of the option to select.
- **deselect(option)**: Deselects an option based on its value. `option` is the value of the option to deselect.
- **remove(option)**: Alias for `deselect(option)`.
- **selectAll()**: Selects all options. Only applicable if `mode` is set to `tags` or `multiple`.
- **clear()**: Deselects all selected options.
- **clearSearch()**: Clears the current search query.
- **refreshOptions(callback)**: Refreshes the async options list. `callback` is an optional function to be called after refresh.
- **setPointer(option)**: Points an option based on its value. `option` is the value of the option to point.
- **focus()**: Programmatically opens and focuses the multiselect element.
```
--------------------------------
### Vue Multiselect: Initial Async Option Loading
Source: https://github.com/vueform/multiselect/blob/main/README.md
Determines whether async options should be loaded when the component initializes. Set to true if you need to load initial values that require fetching corresponding objects from the async source.
```html
```
--------------------------------
### Configuring Multiselect Options
Source: https://github.com/vueform/multiselect/blob/main/README.md
Defines how to provide options to the multiselect component. Options can be a simple array, an object, an array of objects with custom value, label, and disabled properties, or an asynchronous function returning options.
```vue
```
```javascript
export default {
data() {
return {
value: null,
options: [
{ id: 1, name: 'Vue.js' },
{ id: 2, name: 'React' },
{ id: 3, name: 'Angular' }
]
}
}
}
```
--------------------------------
### Multiselect with Groups in Vue
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates grouping options in a multiselect component. The `groups` prop is set to true, and options are structured as an array of objects, each containing a `label` and an `options` array.
```vue
```
--------------------------------
### Vue Multiselect Async Options with Default Values
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates asynchronous loading of options in Vue-Multiselect using `resolveOnLoad: false` and `object: true`. This allows setting default values as objects with `label` and `value` properties, which is necessary when options are not resolved on mount. It requires a `fetchLanguages` function and Vue's data management.
```vue
```
--------------------------------
### Tags with Search and Create Option in Vue
Source: https://github.com/vueform/multiselect/blob/main/README.md
Implements a tag input field using Vue-Multiselect, allowing users to search through options, create new tags, and select multiple items. Options are provided as an array of objects.
```vue
```
--------------------------------
### Tags with Async Options in Vue
Source: https://github.com/vueform/multiselect/blob/main/README.md
Sets up a tag input that loads options asynchronously. Similar to autocomplete with async options, it uses properties like `filter-results: false`, `min-chars: 1`, and `resolve-on-load: false` to trigger asynchronous fetching. The `options` prop takes an async function.
```vue
```
--------------------------------
### Grouping Options in Multiselect
Source: https://github.com/vueform/multiselect/blob/main/README.md
Illustrates how to group options within the multiselect component. This involves setting the `groups` prop to true and defining `groupLabel` and `groupOptions` properties to structure the grouped data.
```vue
```
```javascript
export default {
data() {
return {
value: null,
groupedOptions: [
{
groupName: 'Web Frameworks',
items: [
{ id: 1, name: 'Vue.js' },
{ id: 2, name: 'React' }
]
},
{
groupName: 'JavaScript Libraries',
items: [
{ id: 3, name: 'jQuery' },
{ id: 4, name: 'Lodash' }
]
}
]
}
}
}
```
--------------------------------
### Autocomplete with Async Options in Vue
Source: https://github.com/vueform/multiselect/blob/main/README.md
Configures an autocomplete input that fetches options asynchronously based on user input. It uses `filter-results: false`, `min-chars: 1`, `resolve-on-load: false`, and `delay: 0` for immediate fetching. The `options` prop accepts an async function.
```vue
```
--------------------------------
### Controlling Multiselect Behavior with Props
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates the use of various props to control the behavior of the multiselect, such as `disabled`, `required`, `max` selections, and `infinite` scrolling for large option lists.
```vue
```
--------------------------------
### Single Select with Vue
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates a basic single select implementation using Vue-Multiselect with an array of string options. The selected value is bound to the 'value' model.
```vue
```
--------------------------------
### Slots
Source: https://github.com/vueform/multiselect/blob/main/README.md
Customizable template sections for rendering different parts of the Multiselect component.
```APIDOC
## Slots
| Slot Name | Attributes | Description |
|----------------|--------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `placeholder` | | Rendered as placeholder when the multiselect has no value and the `placeholder` prop is defined. |
| `afterlist` | | Rendered after the options list. |
| `beforelist` | | Rendered before the options list. |
| `multiplelabel`| `values` | Rendered when using `multiple` mode and options are selected. By default, it renders the return value of the `multipleLabel` function. |
| `nooptions` | | Rendered when the options list is empty. By default, it renders `noOptionsText`. |
| `noresults` | | Rendered when there are no search results. By default, it renders `noResultsText`. |
| `grouplabel` | `group`, `isPointed`, `isSelected` | Renders a group label for options. `isPointed(option)` and `isSelected(option)` are functions to check the state of an option. |
| `option` | `option`, `isPointed`, `isSelected`, `search` | Renders an option in the options list. `isPointed(option)` and `isSelected(option)` are functions to check the state of an option. |
| `singlelabel` | `value` | Rendered when using `single` mode and an option is selected. By default, it renders the `:label` of the selected option. |
| `tag` | `option`, `handleTagRemove`, `disabled` | Renders a tag when using `tags` mode. If `disabled`, the remove icon should not be displayed. `handleTagRemove` should be used to trigger removal. |
| `caret` | `handleCaretClick`, `isOpen` | Renders a small triangle on the right side of the multiselect. Set `pointer-events: false` to `handleCaretClick` when `isOpen: false` for original behavior. |
| `clear` | `clear` | Renders a remove icon if the multiselect has a value. The `clear` method should be used on the `mousedown` event. |
| `spinner` | | Renders a loader icon when async options are being fetched. |
| `infinite` | | Renders a loader icon when infinite scroll is in progress. |
> Note: Property names are normalized back to lowercase when used in the DOM due to Vue's handling.
```
--------------------------------
### Focusing the Multiselect Input Element Programmatically
Source: https://github.com/vueform/multiselect/blob/main/README.md
Illustrates how to focus the underlying input element of the Multiselect component. This is achieved by accessing the component's root element via '$el' and calling its 'focus()' method.
```js
mounted() {
this.$refs.multiselect.$el.focus()
}
```
--------------------------------
### Customizing Multiselect Labels
Source: https://github.com/vueform/multiselect/blob/main/README.md
Shows how to customize the display of selected options using the `multipleLabel` prop for multiple selection modes. This allows for dynamic text based on the number of selected items.
```vue
```
```javascript
export default {
data() {
return {
value: [],
options: ['Option 1', 'Option 2', 'Option 3']
}
},
methods: {
customMultipleLabel(current, select$) {
return `${current.length} items selected`;
}
}
}
```
--------------------------------
### Importing Tailwind CSS for Multiselect
Source: https://github.com/vueform/multiselect/blob/main/README.md
This Vue template and style block shows how to import the Tailwind CSS theme for Vueform Multiselect into your main application component. By importing the `tailwind.css` file, you enable the styling customizations defined in your `tailwind.config.js` for the multiselect component. This is typically done in the `
```
--------------------------------
### Vue Multiselect with Custom Options and Single Label Slot
Source: https://github.com/vueform/multiselect/blob/main/README.md
Demonstrates how to use custom slots to render options and a single selected label in Vue-Multiselect. This allows for rich visual representation of choices, including images alongside text. It requires the Multiselect component and its associated data.
```vue
{{ value.name }}
{{ option.name }}
```
--------------------------------
### Vue Multiselect: Custom Option Creation Transform
Source: https://github.com/vueform/multiselect/blob/main/README.md
Shows how to transform a created option before it's added to the list using the 'onCreate' prop. This allows modifying the option's value and label, or preventing its addition by returning false.
```javascript
methods: {
customOptionTransformer(newOption, selectInstance) {
// Example: transform to { id: newOption.value, text: newOption.label }
return { id: newOption.value, text: newOption.label };
}
}
```
```html
```
--------------------------------
### Multiselect with Object Options in Vue
Source: https://github.com/vueform/multiselect/blob/main/README.md
Shows a multiselect component where options are provided as an object (key-value pairs). The `mode` is set to 'multiple' and `close-on-select` is false to allow multiple selections without closing the dropdown.
```vue
```
--------------------------------
### Vue Multiselect: Async Option Loading Delay
Source: https://github.com/vueform/multiselect/blob/main/README.md
Configures the delay in milliseconds before refreshing the async option list after the user stops typing. A delay of -1 prevents refresh on query change, and 0 means no delay.
```html
```
--------------------------------
### Styling Multiselect with CSS Variables
Source: https://github.com/vueform/multiselect/blob/main/README.md
This snippet demonstrates how to customize the Vueform Multiselect component by overriding default CSS variables. These variables control various aspects of the component's appearance, such as font size, colors, borders, and spacing. Customizations can be applied globally using the :root selector or on a per-instance basis by applying classes to the Multiselect component.
```css
:root {
--ms-tag-bg: #059669;
--ms-tag-color: #D1FAE5;
--ms-tag-radius: 9999px;
--ms-tag-font-weight: 400;
}
```
--------------------------------
### Advanced Positioning with appendToBody
Source: https://github.com/vueform/multiselect/blob/main/README.md
Explains the experimental `appendToBody` and `appendTo` props for Vue 3, which allow the dropdown list to be appended to the document body or a specific DOM element for better positioning and to avoid overflow issues.
```vue
```
```vue
```
--------------------------------
### Events
Source: https://github.com/vueform/multiselect/blob/main/README.md
Events emitted by the Multiselect component in response to user interactions or state changes.
```APIDOC
## Events
The `select$` parameter in each event is the Multiselect component's instance.
| Event Name | Attributes | Description |
|-----------------|--------------------------|------------------------------------------------------------------------------|
| `@change` | `value`, `select$` | Emitted after the value is changed. |
| `@select` | `value`, `option`, `select$` | Emitted after an option or tag is selected. |
| `@deselect` | `value`, `option`, `select$` | Emitted after an option is deselected or a tag is removed. |
| `@open` | `select$` | Emitted after the option list is opened. |
| `@close` | `select$` | Emitted after the option list is closed. |
| `@search-change`| `query`, `select$` | Emitted after a character is typed into the search field. |
| `@tag` | `query`, `select$` | **Deprecated 2.3.0: use `@create` instead**. Emitted after Enter is pressed when creating a new tag. |
| `@option` | `query`, `select$` | **Deprecated 2.6.0: use `@create` instead**. Emitted after Enter is pressed when creating a new option. |
| `@create` | `query`, `select$` | Emitted after Enter is pressed when creating a new option. |
| `@clear` | `select$` | Emitted when the options are cleared. |
| `@paste` | `Event`, `select$` | Emitted when the value is pasted into the search field. |
| `@keydown` | `Event`, `select$` | Emitted on `keydown`. |
| `@keyup` | `Event`, `select$` | Emitted on `keyup`. |
| `@max` | `select$` | Emitted when the `max` limit is reached in `multiple` or `tags` mode. |
```
--------------------------------
### Vue Multiselect Custom Styling with :classes Prop
Source: https://github.com/vueform/multiselect/blob/main/README.md
Defines custom Tailwind CSS classes for the Vue Multiselect component. This allows for granular control over styling without needing to import the default stylesheet. The prop accepts an object where keys represent component elements and values are corresponding class strings.
```vue