Skip to content

Commit 0c5ff71

Browse files
committed
improve dynamic options docs
1 parent 72e28a6 commit 0c5ff71

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

source/guide/forms.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ If you want user input to be automatically persisted as numbers, you can add a `
9696

9797
## Dynamic Select Options
9898

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:
100100

101101
``` html
102102
<select v-model="selected" options="myOptions"></select>
103103
```
104104

105-
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.
106106

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:
108108

109109
``` js
110110
[
@@ -146,9 +146,25 @@ Will render:
146146
</select>
147147
```
148148

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+
return value.map(function (item) {
154+
return item[keyToExtract]
155+
})
156+
})
157+
```
158+
159+
``` html
160+
<select options="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+
149165
## Input Debounce
150166

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.
152168

153169
``` html
154170
<input v-model="msg" debounce="500">
@@ -164,4 +180,6 @@ new Vue({
164180
})
165181
</script>
166182

167-
Next: [Computed Properties](/guide/computed.html).
183+
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`.
184+
185+
Next: [Computed Properties](/guide/computed.html).

0 commit comments

Comments
 (0)