【发布时间】:2017-11-30 12:05:02
【问题描述】:
我正在尝试在我的 Vue.js 2 应用程序中搜索或过滤 3 个字段 firstname、lastname 和 email。我知道 Vue 2 不像 Vue 1 那样带有内置的过滤器方法,因此我创建了一个只能过滤一个字段的自定义方法。如何将其扩展到多个字段?我尝试过类似filterBy(list, value1, value2, value3) 的方法,但它不起作用。
这是我的代码
<template>
<div class="customers container">
<input class="form-control" placeholder="Enter Last Name" v-
model="filterInput">
<br />
<table class="table table-striped">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-for="customer in filterBy(customers, filterInput)">
<td>{{customer.first_name}}</td>
<td>{{customer.last_name}}</td>
<td>{{customer.email}}</td>
<td><router-link class="btn btn-default" v-bind:to="'/customer/'+customer.id">View</router-link></td></tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
name: 'customers',
data () {
return {
customers: [],
filterInput:'',
}
},
methods: {
fetchCustomers(){
this.$http.get('http://slimapp.dev/api/customers')
.then(function(response){
this.customers = (response.body);
});
},
filterBy(list, value){
value = value.charAt(0).toUpperCase() + value.slice(1);
return list.filter(function(customer){
return customer.last_name.indexOf(value) > -1;
});
},
},
created: function(){
if (this.$route.params.alert) {
this.alert = $route.params.alert
}
this.fetchCustomers();
},
updated: function(){
this.fetchCustomers();
},
components: {
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
【问题讨论】:
-
你可以看看这个 vue 插件 - github.com/freearhey/vue2-filters
标签: javascript vue.js vuejs2 vue-component