如何使用自定义过滤器道具并在其中添加新过滤器?使用这些数据我想找到所有的“失业鸡”#34;并删除所有特殊字符和空格:
data () {
return {
headers: [
{ text: 'Name', value: 'name' },
{ text: 'Animal', value: 'animal' },
{ text: 'Job', value: 'job' },
{ text: 'Age', value: 'age' },
],
items: [
{ name: 'Frozen Yogurt', animal: 'Chicken', job: 'Electrician', age: 24 },
{ name: 'Eclair', animal: 'Cow', job: 'IT Consultant', age:45 },
{ name: 'Cupcake', animal: 'Cow', job: 'Unemployed', age:26 },
{ name: 'Gingerbread', animal: 'Chicken', job: 'Unemployed', age:38 },
{ name: 'Jelly bean', animal: 'Cow', job: 'Fraud Specalist', age:52 },
{ name: 'Lollipop', animal: 'Sheep', job: 'Unemployed', age:18 },
{ name: 'Honeycomb', animal: 'Sheep', job: 'Plumber', age:32 },
{ name: 'Donut', animal: 'Chicken', job: 'Unemployed', age:22 },
{ name: 'KitKat', animal: 'Sheep', job: 'Gym Junkie', age:41 },
]
}
},
答案 0 :(得分:3)
把它放在这里是因为我无法找到文档。我将搜索更改为自定义过滤器,以便您可以使用空格搜索多个字段。这样使用下面的代码就可以搜索"失业的鸡肉"得到你的结果。
new Vue({
el: '#app',
data () {
return {
search: '',
headers: [
{ text: 'Name', value: 'name' },
{ text: 'Animal', value: 'animal' },
{ text: 'Job', value: 'job' },
{ text: 'Age', value: 'age' },
],
items: [
{ name: 'Frozen Yogurt', animal: 'Chicken', job: 'Electrician', age: 24 },
{ name: 'Eclair', animal: 'Cow', job: 'IT Consultant', age:45 },
{ name: 'Cupcake', animal: 'Cow', job: 'Unemployed', age:26 },
{ name: 'Gingerbread', animal: 'Chicken', job: 'Unemployed', age:38 },
{ name: 'Jelly bean', animal: 'Cow', job: 'Fraud Specalist', age:52 },
{ name: 'Lollipop', animal: 'Sheep', job: 'Unemployed', age:18 },
{ name: 'Honeycomb', animal: 'Sheep', job: 'Plumber', age:32 },
{ name: 'Donut', animal: 'Chicken', job: 'Unemployed', age:22 },
{ name: 'KitKat', animal: 'Sheep', job: 'Gym Junkie', age:41 },
]
}
},
methods: {
customFilter(items, search, filter) {
//this custom filter will do a multi-match separated by a space.
//e.g
if (!search) { return items } //if the search is empty just return all the items
function new_filter (val, search) {
return val !== null &&
['undefined', 'boolean'].indexOf(typeof val) === -1 &&
val.toString().toLowerCase().replace(/[^0-9a-zA-Z]+/g,"").indexOf(search) !== -1
}
let needleAry = search.toString().toLowerCase().split(" ").filter(x => x);
//whenever we encounter a space in our search we will filter for both the first and second word (or third word)
return items.filter(row => needleAry.every(needle => Object.keys(row).some(key => new_filter(row[key],needle))));
//foreach needle in our array cycle through the data (row[key]) in the current row looking for a match.
// .some will return true and exit the loop if it finds one it will return false if it doesnt
// .every will exit the loop if we dont find a match but will return true if all needles match
// .filter the rows on each match
}
}
})

<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify/dist/vuetify.js"></script>
</head>
<body>
<div id="app">
<v-app id="inspire">
<v-card>
<v-card-title>
Animals in the workforce
<v-spacer></v-spacer>
<v-text-field
append-icon="search"
label="Search"
single-line
hide-details
v-model="search"
></v-text-field>
</v-card-title>
<v-data-table
hide-actions
:headers="headers"
:items="items"
:search="search"
:custom-filter="customFilter"
>
<template slot="items" scope="props">
<td class="text-xs-right">{{ props.item.name }}</td>
<td class="text-xs-right">{{ props.item.animal }}</td>
<td class="text-xs-right">{{ props.item.job }}</td>
<td class="text-xs-right">{{ props.item.age }}</td>
</template>
</v-data-table>
</v-card>
</v-app>
</div>
</body>
</html>
&#13;