我正在尝试在Vuejs
中构建一个表过滤器,根据需求过滤表,我有两个单独的输入字段,它们过滤一个公共表,我的表代码是这样的:
<div class="col-sm-4">
<div class="form-group">
<label class="control-label">Search by company name</label>
<input type="text" v-model="search_by_name" placeholder="Search by company name" class="form-control">
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<label class="control-label">Search by email</label>
<input type="text" v-model="search_by_email" placeholder="Search by email" class="form-control">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="control-label" for="tags">Search by tags</label>
<select name="status" id="tags" class="form-control">
<option value="1" selected="">Completed</option>
<option value="0">Pending</option>
</select>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped">
<tbody>
<tr v-for="item in tableFilter">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.city }}</td>
<td>{{ item.number }}</td>
<td>{{ item.email }}</td>
<td><router-link :to="{name: 'company-update', params: {id: item.id}}"><i class="fa fa-pencil-square-o text-navy"></i></router-link></td>
<td><a @click.prevent="deleteCompany(item.id)"><i class="fa fa-trash-o text-navy"></i></a></td>
</tr>
</tbody>
</table>
</div>
对于过滤我在我的vue实例中有以下内容:
export default {
data(){
return {
search_by_name: '',
search_by_email: '',
model: {},
columns: {},
}
},
props: ['source'],
created() {
this.fetchIndexData()
},
methods: {
fetchIndexData() {
axios.get('/companies').then(response => {
Vue.set(vm.$data, 'model', response.data.model)
Vue.set(vm.$data, 'columns', response.data.columns)
}).catch(response => {
// console.log(response)
})
},
findBy: function(list, value, column) {
return list.filter(function(item) {
return item[column].includes(value)
})
}
},
computed: {
tableFilter: function () {
if(this.model.data)
{
return this.findBy(this.model.data, this.search_by_name, 'name')
}
}
}
}
目前,名称搜索工作正常,我希望将此搜索与电子邮件搜索绑定,如this.search_by_email
或drop-down
,如html部分所述。
答案 0 :(得分:3)
您需要在选择
中为值建模 <select name="status" id="tags" class="form-control" v-model="pending_or_completed">
<option value="1">Completed</option>
<option value="0">Pending</option>
</select>
您需要计算机来测试所有条件
tableFilter() {
if (this.model.data) {
return this.model.data.filter((item) =>
item.name.includes(this.search_by_name)
&& item.email.includes(this.search_by_email)
&& item.status === this.pending_or_completed);
}
}
new Vue({
el: '#app',
data: {
search_by_name: 'name',
search_by_email: '',
pending_or_completed: '1',
model: {
data: [{
id: 1,
name: 'one',
email: 'foo@one.com',
status: '1'
},
{
id: 2,
name: 'two',
email: 'bar@two.com',
status: '0'
},
{
id: 5,
name: 'five',
email: 'the@five.edu',
status: '0'
}
]
}
},
computed: {
tableFilter() {
if (this.model.data) {
return this.model.data.filter((item) => item.name.includes(this.search_by_name) && item.email.includes(this.search_by_email) && item.status === this.pending_or_completed);
}
}
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.min.js"></script>
<div id="app">
<div class="col-sm-4">
<div class="form-group">
<label class="control-label">Search by company name</label>
<input type="text" v-model="search_by_name" placeholder="Search by company name" class="form-control">
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<label class="control-label">Search by email</label>
<input type="text" v-model="search_by_email" placeholder="Search by email" class="form-control">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label class="control-label" for="tags">Search by tags</label>
<select name="status" id="tags" class="form-control" v-model="pending_or_completed">
<option value="1">Completed</option>
<option value="0">Pending</option>
</select>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped">
<tbody>
<tr v-for="item in tableFilter">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.city }}</td>
<td>{{ item.number }}</td>
<td>{{ item.email }}</td>
<td>
<router-link :to="{name: 'company-update', params: {id: item.id}}"><i class="fa fa-pencil-square-o text-navy"></i></router-link>
</td>
<td><a @click.prevent="deleteCompany(item.id)"><i class="fa fa-trash-o text-navy"></i></a></td>
</tr>
</tbody>
</table>
</div>
</div>