You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/guide/forms.md
+23-5Lines changed: 23 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -96,15 +96,15 @@ If you want user input to be automatically persisted as numbers, you can add a `
96
96
97
97
## Dynamic Select Options
98
98
99
-
When you need to dynamically render a list of options for a `<select>` element, it's recommended to use an `options` attribute together with `v-model`:
99
+
When you need to dynamically render a list of options for a `<select>` element, it's recommended to use an `options` attribute together with `v-model` so that when the options change dynamically, `v-model` is properly synced:
In your data, `myOptions` should be an keypath/expression that points to an Array to use as its options. The Array can contain plain strings, or contain objects.
105
+
In your data, `myOptions` should be an keypath/expression that points to an Array to use as its options.
106
106
107
-
The object can be in the format of `{text:'', value:''}`. This allows you to have the option text displayed differently from its underlying value:
107
+
The Array can contain plain strings or objects. The object can be in the format of `{text:'', value:''}`. This allows you to have the option text displayed differently from its underlying value:
108
108
109
109
```js
110
110
[
@@ -146,9 +146,25 @@ Will render:
146
146
</select>
147
147
```
148
148
149
+
It's quite likely that your source data does not come in this desired format, and you will have to transform the data in order to generate dynamic options. In order to DRY-up the transformation, the `options` param supports filters, and it can be helpful to put your transformation logic into a reusable [custom filter](/guide/custom-filter.html):
150
+
151
+
```js
152
+
Vue.filter('extract', function (value, keyToExtract) {
153
+
returnvalue.map(function (item) {
154
+
return item[keyToExtract]
155
+
})
156
+
})
157
+
```
158
+
159
+
```html
160
+
<selectoptions="users | extract 'name'"></select>
161
+
```
162
+
163
+
The above filter transforms data like `[{ name: 'Bruce' }, { name: 'Chuck' }]` into `['Bruce', 'Chuck']` so it becomes properly formatted.
164
+
149
165
## Input Debounce
150
166
151
-
The `debounce` param allows you to set a minimum delay after each keystroke before an update is executed. This can be useful when you are performing expensive operations on each update, for example making an Ajax request for type-ahead autocompletion.
167
+
The `debounce` param allows you to set a minimum delay after each keystroke before the input's value is synced to the model. This can be useful when you are performing expensive operations on each update, for example making an Ajax request for type-ahead autocompletion.
Note that the `debounce` param does not debounce the user's input events: it debounces the "write" operation to the underlying data. Therefore you should use `vm.$watch()` to react to data changes when using `debounce`.
0 commit comments