[英]Filter list by selected option in vue.js 2
Im trying out vue.js and cannot really figure out how i can filter a rendered list of items by simply select an option in a box. 我正在尝试vue.js,但无法真正弄清楚如何通过仅在框中选择一个选项来过滤呈现的项目列表。
Each list item has a pric property. 每个列表项都有一个pric属性。 eg.
例如。 $200, $500, $600 etc.
$ 200,$ 500,$ 600等。
My select box options each has a price range. 我的选择框选项每个都有一个价格范围。 eg 0 - $250, $250 - $500, $500 - $750.
例如0-$ 250,$ 250-$ 500,$ 500-$ 750。
When i for example choose $200 - $500 in the select box i only want the items in the list which has a price somewhere in between that range to be visible. 例如,当我在选择框中选择$ 200-$ 500时,我只希望列表中价格在该范围内的商品可见。
html: HTML:
<select>
<option>
Price 0 - 250
</option>
<option>
Price 250 - 500
</option>
<option>
Price 500 - 750
</option>
<option>
Price 750 - 1000
</option>
</select>
template: 模板:
<li v-for="(item, index) in items">
{{ item.price }} // some price in $. eg. $ 340. different for each
item
</li>
Since i first misunderstood the question I've updated the original answer. 自从我第一次误解了这个问题以来,我已经更新了原始答案。 Anyway, here is a jsfiddle that does what i believe you are trying to do.
无论如何, 这是一个我相信您正在尝试做的jsfiddle。
Vue.filter('currency', function (value) {
return '€ ' + parseFloat(value).toFixed(2);
});
var demo = new Vue({
el: '#demo',
data: {
items: [5,20],
limits: [{min: 1, max: 10}, {min:11, max: 25}, {min:26, max: 50}],
option: 0
},
computed: {
selectedList () {
var ul = [];
for(i =0; i < this.items.length; i++) {
if (this.items[i] > this.limits[this.option].min && this.items[i] < this.limits[this.option].max) {
ul.push(this.items[i])
}
}
return ul;
}
}
})
I've placed your min/max values in an array so we can loop through these with our select element. 我已将您的最小值/最大值放置在数组中,因此我们可以使用select元素遍历这些值。 Than we create a computed array, this array returns all values between the selected min/max.
与创建计算数组相比,此数组返回选定的最小值/最大值之间的所有值。
I've also added a filter to add the currency symbol. 我还添加了一个过滤器来添加货币符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.