alw*_*leh 16 frontend vue.js vuetify.js
截至发布日期,我找不到任何文档来使用数据表中的"自定义过滤器"道具.
我只是想创建一个自定义过滤器来按标头过滤我的数据表.我有一个下拉列表,当用户单击下拉列表的其中一个选项时,它将过滤一个特定标题的列表.
示例:下拉选项:食物类型:水果,肉类,蔬菜
如果我选择下拉肉作为肉,它应该只显示我的猪肉和鸡腿.
tha*_*ksd 23
查看Github上的代码,看起来customFilterprop用于覆盖用于确定如何将filterprop应用于表中的项的默认方法.
默认customFilter方法将filter函数应用于每个项目对象的每个属性名称,并过滤掉任何不包含一个通过过滤器的属性名称的项目:
Run Code Online (Sandbox Code Playgroud)customFilter: { type: Function, default: (items, search, filter) => { search = search.toString().toLowerCase() return items.filter(i => ( Object.keys(i).some(j => filter(i[j], search)) )) } },
如果您希望阻止任何列包含在过滤器中,或者您确实希望防止过滤掉特定行,则可能需要覆盖此函数.
您会注意到该方法还取决于searchprop,它必须是一个字符串.
总而言之,你真的不需要使用那个道具来做你想做的事.您应该创建一个计算属性,根据您的下拉值过滤项目,并将该计算属性作为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);
})
}
}
})Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
Sot*_*oth 10
您也可以像这样使用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));
}
}
})Run Code Online (Sandbox Code Playgroud)
<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="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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22507 次 |
| 最近记录: |