Tro*_*sta 3 javascript jquery vue.js vuejs2
我正在尝试在Vue中创建一个精选组.
小提琴:https://jsfiddle.net/Tropicalista/vwjxc5dq/
我试过这个:
<optgroup v-for="option in options" v-bind:label="option">
<option v-for="sub in option" v-bind:value="option.value">
{{ sub.text }}
</option>
</optgroup>
Run Code Online (Sandbox Code Playgroud)
我的数据:
data: {
selected: 'A',
options: {
First: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' }
],
Second: [
{ text: 'Three', value: 'C' }
]
}
}
Run Code Online (Sandbox Code Playgroud)
tha*_*ksd 10
您将label属性绑定到option,这是一个数组.你想要的是绑定到对象的键.
您可以通过在v-for指令中指定第二个参数来获取每个选项的键:
<optgroup v-for="(option, key) in options" v-bind:label="key">
Run Code Online (Sandbox Code Playgroud)
我还要重命名你的options财产optionGroups以避免进一步混淆:
data: {
selected: 'A',
optionGroups: {
First: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' }
],
Second: [
{ text: 'Three', value: 'C' }
]
}
}
Run Code Online (Sandbox Code Playgroud)
这样,模板会更有意义:
<optgroup v-for="(group, name) in optionGroups" :label="name">
<option v-for="option in group" :value="option.value">
{{ option.text }}
</option>
</optgroup>
Run Code Online (Sandbox Code Playgroud)