如何在数据表中使用“自定义过滤器”属性?或者如何创建一个自定义过滤器来按标题进行过滤?
问题描述:
截至发布日期,我找不到任何文档在数据表中使用“自定义过滤器”属性。如何在数据表中使用“自定义过滤器”属性?或者如何创建一个自定义过滤器来按标题进行过滤?
我只是想创建一个自定义过滤器来通过标头过滤我的数据表。 我有一个下拉菜单,当用户点击其中一个下拉选项时,它会过滤一个特定标题的列表。
例子: 下拉选项: 食品类:水果,肉类,蔬菜
- Bakchoi(蔬菜)
- 猪(肉)
- 鸡大腿(肉)
- 西瓜(果)
如果我选择下拉菜单作为肉,它应该只显示我猪肉和别致肯大腿。
答
看着code on Github,它看起来像customFilter
prop用于覆盖用于确定如何将filter
prop应用于表中的项目的默认方法。
默认customFilter
方法应用filter
功能,每个项目对象的每个属性名称,并过滤掉不包括通过过滤器,一个属性名称的任何项目:
customFilter: { type: Function, default: (items, search, filter) => { search = search.toString().toLowerCase() return items.filter(i => ( Object.keys(i).some(j => filter(i[j], search)) )) } },
你如果您想阻止任何列包含在过滤器中,或者您总是希望防止被过滤掉的特定行,您可能想要覆盖此功能。
您会注意到该方法也取决于search
prop,该prop必须是字符串。
所有这一切,你真的不需要使用该道具来做你想做的事情。您只需制作一个计算属性即可根据您的下拉值过滤项目,并将该计算属性作为items
支柱。
下面是一个例子:
new Vue({
el: '#app',
data() {
return {
food: [
{ name: 'Bakchoi', type: 'vegetable', calories: 100 },
{ name: 'Pork', type: 'meat', calories: 200 },
{ name: 'Chicken Thigh', type: 'meat', calories: 300 },
{ name: 'Watermelon', type: 'fruit', calories: 10 },
],
headers: [
{ text: 'Name', align: 'left', value: 'name' },
{ text: 'Food Type', align: 'left', value: 'type' },
{ text: 'Calories', align: 'left', value: 'calories' },
],
foodType: null,
};
},
computed: {
filteredItems() {
return this.food.filter((i) => {
return !this.foodType || (i.type === this.foodType);
})
}
}
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vuetify.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/vuetify.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons">
<div id="app">
<v-app>
<v-select
label="Food Type"
:items="['vegetable', 'meat', 'fruit']"
v-model="foodType"
></v-select>
<v-data-table
:headers="headers"
:items="filteredItems"
hide-actions
>
<template slot="items" scope="{ item }">
<td>{{ item.name }}</td>
<td>{{ item.type }}</td>
<td>{{ item.calories }}</td>
</template>
</v-data-table>
</v-app>
</div>
答
你也可以去像这样customFilter办法,我已经限制了搜索到的类型字段。
new Vue({
el: '#app',
data() {
return {
food: [
{ name: 'Bakchoi', type: 'vegetable', calories: 100 },
{ name: 'Pork', type: 'meat', calories: 200 },
{ name: 'Chicken Thigh', type: 'meat', calories: 300 },
{ name: 'Watermelon', type: 'fruit', calories: 10 },
],
headers: [
{ text: 'Name', align: 'left', value: 'name' },
{ text: 'Food Type', align: 'left', value: 'type' },
{ text: 'Calories', align: 'left', value: 'calories' },
],
search: '',
};
},
methods: {
customFilter(items, search, filter) {
search = search.toString().toLowerCase()
return items.filter(row => filter(row["type"], search));
}
}
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vuetify.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/vuetify.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons">
<div id="app">
<v-app>
<v-select
label="Food Type"
:items="['vegetable', 'meat', 'fruit']"
v-model="search"
></v-select>
<v-data-table
:headers="headers"
:items="food"
:search="search"
:custom-filter="customFilter"
hide-actions
>
<template slot="items" scope="{ item }">
<td>{{ item.name }}</td>
<td>{{ item.type }}</td>
<td>{{ item.calories }}</td>
</template>
</v-data-table>
</v-app>
</div>
你摸不着头脑?我正在寻找相同的信息。 – sterling