查看 Github 上的代码1,看起来 customFilter
属性用于覆盖默认方法,该方法确定如何将 filter
属性应用于表格中的项目。
默认的 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”属性。
话虽如此,您实际上不需要使用该属性来做您想做的事情。您只需创建一个计算属性来根据下拉列表值过滤项目,并将该计算属性作为“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/vue@2.4.2/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify@0.15.2/dist/vuetify.js"></script>
<link rel="stylesheet" href="https://unpkg.com/vuetify@0.15.2/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>
- 这个答案是在Vuetify版本为v0.15.2时编写的。 该组件在该版本下的源代码可以在此处找到。