【发布时间】:2016-04-03 16:13:01
【问题描述】:
在 vue.js 中,如何使用针对表上特定列的多个过滤器来过滤表。
例如,如果我有两个搜索字段 name 和 age 我如何绑定它们以搜索下表中的相应列。因此,如果用户在名称输入中输入名称,它应该只在名称列上搜索名称。如果用户也输入了一个年龄,那么它应该形成一个and 条件并在年龄列中搜索一个年龄。目前过滤器只是搜索整个表。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>The Vast World of Vue.js</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body id="demo" class="container">
<input v-model="name" class="form-control" placeholder="search by name"><br>
<input v-model="age" class="form-control" placeholder="search by age">
<table class="table table-striped">
<thead>
<tr>
<th >
Name
</th>
<th >
Age
</th>
</thead>
<tbody>
<tr v-for="item in people | filterBy name | filterBy age">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
</tr>
</tbody>
</table>
<script src="http://vuejs.org/js/vue.js"></script>
<script>
new Vue({
el: '#demo',
name: '',
age: '',
data: {
people: [
{ name: 'John', age: 50 },
{ name: 'Jane', age: 22 },
{ name: 'Paul', age: 34 },
{ name: 'Kate', age: 15 },
]
}
});
</script>
</body>
</html>
【问题讨论】: