发布于 2016-07-07 13:14:25
在0.12中,您可以使用带有选项param的过滤器。文档显示的语法看起来与过滤v相同。
下面是一个显示filterBy
的示例(使用0.12.16版本):
var app = new Vue({
el: '#app',
data: {
selected: '',
options: [
{ text: '1', value: 1, show: true },
{ text: '2', value: 2, show: false },
{ text: '3', value: 3, show: true },
{ text: '4', value: 4, show: true },
{ text: '5', value: 5, show: false },
]
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.16/vue.js"></script>
<div id="app">
Filter by 'show' <br>
<select v-model="selected" options="options | filterBy true in 'show'"></select>
</div>
注意:用于
options
的<select v-model>
参数是在1.0中被弃用。在1.0中,您可以在v-for
中直接使用<select>
。可以嵌套v-for
以使用optgroup。
发布于 2016-07-12 22:34:23
看看这把小提琴https://jsfiddle.net/tronsfey/4zz6zrxv/5/。
<div id="app">
<input type="text" v-model="keyword">
<select name="test" id="test">
<optgroup v-for="group in list | myfilter keyword" label="{{group.label}}">
<option value="{{item.price}}" v-for="item in group.data">{{item.text}}</option>
</optgroup>
</select>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.js"></script>
new Vue({
el: '#app',
data: function () {
return {
keyword : '',
list: [
{
label:'A',
data:[
{price: 3, text: 'aa'},
{price: 2, text: 'bb'}
]
},
{
label:'B',
data:[
{price: 5, text: 'cc'},
{price: 6, text: 'dd'}
]
}
]
}
},
filters : {
myfilter : function(value,keyword){
return (!keyword || keyword === 'aaa') ? value : '';
}
}
})
您可以使用v-for创建optgroup,然后使用filterBy或自定义筛选器(就像小提琴中的那样)来过滤选项数据。
希望能帮上忙。
https://stackoverflow.com/questions/38252680
复制