重置筛选数据 Vue.js
Reset filtered data Vue.js
我需要实现一个按钮,用于在我的应用程序中删除过滤器。该应用程序是在 Vue 上编写的。过滤器本身已实现,但我不知道如何实现它们的重置。
<template>
<div id="app">
<input type="text" v-model="search">
<select name="sort" v-model="sort">
<option v-for="option in options" :value="option.value" :label="option.label"></option>
</select>
<table>...</table>
</div>
</template>
<script>
import goodsList from './api/data';
export default {
name: 'app',
data() {
return {
search: '',
sort: '',
options: [
{ label: 'Default', value: 'none' },
{ label: 'Brand', value: 'brand' },
{label: 'Price', value: 'price'}
],
goods: goodsList,
}
},
computed: {
filteredList() {
let filteredGoods = this.goods.filter( item => {
return item.name.toLowerCase().includes(this.search.toLowerCase());
});
switch (this.sort) {
case 'brand':
filteredGoods.sort((a, b) => a.brand.localeCompare(b.brand));
break;
case 'price':
filteredGoods.sort((a, b) => parseInt(a.price - b.price));
break;
}
return filteredGoods;
}
},
}
</script>
您将需要一个重置功能,它将默认选择的值分配给例如:'none' 到 v-model 'sort'。由于它是双向绑定,更改 'sort' 变量的值最终将重置所选选项。
要添加的功能:
resetOptions: function () {
this.sort='none';
}
我需要实现一个按钮,用于在我的应用程序中删除过滤器。该应用程序是在 Vue 上编写的。过滤器本身已实现,但我不知道如何实现它们的重置。
<template>
<div id="app">
<input type="text" v-model="search">
<select name="sort" v-model="sort">
<option v-for="option in options" :value="option.value" :label="option.label"></option>
</select>
<table>...</table>
</div>
</template>
<script>
import goodsList from './api/data';
export default {
name: 'app',
data() {
return {
search: '',
sort: '',
options: [
{ label: 'Default', value: 'none' },
{ label: 'Brand', value: 'brand' },
{label: 'Price', value: 'price'}
],
goods: goodsList,
}
},
computed: {
filteredList() {
let filteredGoods = this.goods.filter( item => {
return item.name.toLowerCase().includes(this.search.toLowerCase());
});
switch (this.sort) {
case 'brand':
filteredGoods.sort((a, b) => a.brand.localeCompare(b.brand));
break;
case 'price':
filteredGoods.sort((a, b) => parseInt(a.price - b.price));
break;
}
return filteredGoods;
}
},
}
</script>
您将需要一个重置功能,它将默认选择的值分配给例如:'none' 到 v-model 'sort'。由于它是双向绑定,更改 'sort' 变量的值最终将重置所选选项。
要添加的功能:
resetOptions: function () {
this.sort='none';
}