在早期版本的vue.js
中,您可以使用optgroups
(如this)創建一個動態選擇列表。在vue 1.x中選擇`optgroup`
在較新版本的vue中,文檔在選項中顯示爲使用v-for
,看起來他們不再支持optgroups
。有沒有解決方法?
在早期版本的vue.js
中,您可以使用optgroups
(如this)創建一個動態選擇列表。在vue 1.x中選擇`optgroup`
在較新版本的vue中,文檔在選項中顯示爲使用v-for
,看起來他們不再支持optgroups
。有沒有解決方法?
創建optgroups
列表的最簡單方法是嵌套v-for
。對optgroup
使用外部循環,對option
使用內部循環。這裏有一個例子:
var app = new Vue({
el: '#app',
data: {
selected: '',
optgroups: [
{ label: 'A', options: ['a', 'b']},
{ label: 'B', options: ['c', 'd']}
]
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.js"></script>
<div id="app">
<select v-model="selected">
<optgroup :label="group.label" v-for="group in optgroups">
<option :value="option" v-for="option in group.options">{{ option }}</option>
</optgroup>
</select>
</div>
@ asemahle--用'filterBy'完成這項工作嗎?請參閱以下錯誤:https://jsfiddle.net/y106z528/ – user101289
語法是'filterBy VALUE in PROPERTY'。你的小提琴在'OBJECT'中'過濾價值'。 [這是你的小提琴的一個分支](https://jsfiddle.net/asemahle/1uar6oc1/)。您需要添加額外的邏輯來隱藏空的optgroups。 – asemahle
你不能使用嵌套的'V-for's?外'v-for'來處理'optgroup's和內循環來填充'option's? –