|
1 |
| -# Selecting Values |
| 1 | +## Getting / Setting |
2 | 2 |
|
3 |
| -The most common use case for `vue-select` is to have the chosen value synced with a parent component. `vue-select` takes advantage of the `v-model` syntax to sync values with a parent. |
| 3 | +### `v-model` |
| 4 | + |
| 5 | +The most common use case for `vue-select` is to have the chosen value synced with a parent component. `vue-select` |
| 6 | +takes advantage of the `v-model` syntax to sync values with a parent. |
4 | 7 |
|
5 | 8 | ```html
|
6 |
| -<v-select v-model="selected"></v-select> |
| 9 | +<v-select v-model="selected" /> |
7 | 10 | ```
|
8 | 11 |
|
9 |
| -<CodePen url="Kqxbjw" height="250"/> |
| 12 | +### `value` prop & `input` event |
10 | 13 |
|
11 |
| -If you don't require the `value` to be synced, you can also pass the prop directly: |
| 14 | +If you don't require the `value` to be synced, but you need to preselect a value, you can use the `value` prop. It will |
| 15 | +accept strings, numbers or objects. If you're using a `multiple` v-select, you'll want to pass an array. |
12 | 16 |
|
13 | 17 | ```html
|
14 |
| -<v-select :value="selected"></v-select> |
| 18 | +<v-select :value="selected" /> |
15 | 19 | ```
|
16 | 20 |
|
17 |
| -This method allows you to pre-select a value(s), without syncing any changes to the parent component. This is also very useful when using a state management tool, like Vuex. |
18 |
| - |
19 |
| -## Single/Multiple Selection |
20 |
| - |
21 |
| -By default, `vue-select` supports choosing a single value. If you need multiple values, use the `multiple` prop: |
| 21 | +The `value` prop is very useful when using a state management tool, like Vuex. `vue-select` will emit an `input` event |
| 22 | +any time a value changes. |
22 | 23 |
|
23 | 24 | ```html
|
24 |
| -<v-select multiple v-model="selected"></v-select> |
| 25 | +<v-select :value="selected" @input="setSelected" /> |
25 | 26 | ```
|
26 | 27 |
|
27 |
| -<CodePen url="opMGro" height="250"/> |
28 |
| - |
29 |
| -## Tagging |
30 |
| - |
31 |
| -To allow input that's not present within the options, set the `taggable` prop to true. |
32 |
| -If you want new tags to be pushed to the options list, set `push-tags` to true. |
33 |
| - |
34 |
| -<CodePen url="XVoWxm" height="350"/> |
35 |
| - |
36 |
| -## Return a Single Key from an Object |
37 |
| - |
38 |
| -When the `options` array contains objects, `vue-select` returns the whole object as dropdown value upon selection. You can specify your own `index` prop to return only the value contained in the specific property. |
39 |
| - |
40 |
| -For example, consider an object with `value` and `label` properties: |
41 |
| - |
42 |
| -```json |
43 |
| -{ |
44 |
| - value: "CA", |
45 |
| - label: "Canada" |
| 28 | +```js |
| 29 | +methods: { |
| 30 | + setSelected(value) { |
| 31 | + // do something with selected value |
| 32 | + } |
46 | 33 | }
|
47 | 34 | ```
|
| 35 | +## Transforming Selections |
| 36 | + |
| 37 | +When the `options` array contains objects, `vue-select` returns the whole object as dropdown value upon selection. |
| 38 | + |
| 39 | +If you need to return a single key, or transform the data before it is synced, `vue-select` provides a `reduce` callback |
| 40 | + that allows you to transform a selected option before it is passed to the `@input` event. Consider this data structure: |
| 41 | + |
| 42 | + ```js |
| 43 | + let options = [{code: 'CA', country: 'Canada'}, ...]; |
| 44 | + ``` |
| 45 | + |
| 46 | + If we want to display the `country`, but return the `code` to `v-model`, we can use the `reduce` prop to receive |
| 47 | + only the data that's required. |
| 48 | + |
| 49 | + ```html |
| 50 | + <v-select :options="options" :reduce="country => country.code" label="country" /> |
| 51 | + ``` |
| 52 | + |
| 53 | +The `reduce` property also works well when you have a deeply nested value: |
| 54 | + |
| 55 | + ``` |
| 56 | + { |
| 57 | + country: 'canada', |
| 58 | + meta: { |
| 59 | + id: '1', |
| 60 | + code: 'ca' |
| 61 | + } |
| 62 | + } |
| 63 | + ``` |
| 64 | + |
| 65 | + ```html |
| 66 | + <v-select :options="options" :reduce="country => country.value.id" label="country" /> |
| 67 | + ``` |
| 68 | + |
| 69 | +## Single/Multiple Selection |
48 | 70 |
|
49 |
| -If you wanted to return `CA` in the dropdown when `Canada` is selected, you'd use the `index` key: |
| 71 | +By default, `vue-select` supports choosing a single value. If you need multiple values, use the `multiple` boolean prop, |
| 72 | +much the same way you would on a native `<select>` element. When `multiple` is true, `v-model` or `value` should be |
| 73 | +arrays. |
| 74 | + |
50 | 75 |
|
51 | 76 | ```html
|
52 |
| -<v-select index="value" :options="countries"></v-select> |
| 77 | +<v-select multiple v-model="selected" :options="['foo','bar']" /> |
53 | 78 | ```
|
| 79 | +<v-select multiple :options="['foo','bar']" /> |
0 commit comments