|
| 1 | +--- |
| 2 | +title: 'Pre-selected values' |
| 3 | +--- |
| 4 | + |
| 5 | +# Pre-selected values (single & multi) |
| 6 | + |
| 7 | +The following example demonstrates how to use the `VueSelect` component with pre-selected values. |
| 8 | + |
| 9 | +This can be achieved by setting a value to the `ref` which is passed as a `v-model` to the `VueSelect` component. |
| 10 | + |
| 11 | +<script setup> |
| 12 | +import { ref } from "vue"; |
| 13 | + |
| 14 | +import VueSelect from "../../src"; |
| 15 | + |
| 16 | +const options = [ |
| 17 | + { label: 'Option #1', value: 'option_1' }, |
| 18 | + { label: 'Option #2', value: 'option_2' }, |
| 19 | + { label: 'Option #3', value: 'option_3' }, |
| 20 | + { label: 'Option #4', value: 'option_4' }, |
| 21 | + { label: 'Option #5', value: 'option_5' }, |
| 22 | +]; |
| 23 | + |
| 24 | +const singleSelected = ref("option_1"); |
| 25 | +const multiSelected = ref(["option_3", "option_5"]); |
| 26 | +</script> |
| 27 | + |
| 28 | +<p>Single select:</p> |
| 29 | + |
| 30 | +<VueSelect |
| 31 | + v-model="singleSelected" |
| 32 | + :options="options" |
| 33 | +/> |
| 34 | + |
| 35 | +<p>Multi select:</p> |
| 36 | + |
| 37 | +<VueSelect |
| 38 | + v-model="multiSelected" |
| 39 | + :options="options" |
| 40 | + :is-multi="true" |
| 41 | +/> |
| 42 | + |
| 43 | +## Demo source-code |
| 44 | + |
| 45 | +```vue |
| 46 | +<script setup lang="ts"> |
| 47 | +import { ref } from "vue"; |
| 48 | +import VueSelect from "vue3-select-component"; |
| 49 | +
|
| 50 | +const options = [ |
| 51 | + { label: "Option #1", value: "option_1" }, |
| 52 | + { label: "Option #2", value: "option_2" }, |
| 53 | + { label: "Option #3", value: "option_3" }, |
| 54 | + { label: "Option #4", value: "option_4" }, |
| 55 | + { label: "Option #5", value: "option_5" }, |
| 56 | +]; |
| 57 | +
|
| 58 | +const singleSelected = ref("option_1"); |
| 59 | +const multiSelected = ref(["option_3", "option_5"]); |
| 60 | +</script> |
| 61 | +
|
| 62 | +<template> |
| 63 | + <p>Single select:</p> |
| 64 | +
|
| 65 | + <VueSelect |
| 66 | + v-model="singleSelected" |
| 67 | + :options="options" |
| 68 | + /> |
| 69 | +
|
| 70 | + <p>Multi select:</p> |
| 71 | +
|
| 72 | + <VueSelect |
| 73 | + v-model="multiSelected" |
| 74 | + :options="options" |
| 75 | + :is-multi="true" |
| 76 | + /> |
| 77 | +</template> |
| 78 | +``` |
0 commit comments