This table contains two columns: Name
, and Age
. Currently, the search functionality only filters by name. However, I would like to implement filtering by age using a comparison operator such as <
or >
.
If you want to see the code in action, check out this CodePen link
HTML:
<div id="demo" class="container">
<div class="row">
<div class="col-md-6">
<input v-model="search" class="form-control" placeholder="Enter username to search">
</div>
<div class="col-md-1">
<select class="form-control" v-model="searchOperator">
<option value=">">></option>
<option value="<"><</option>
</select>
</div>
<div class="col-md-5">
<input v-model="searchName" class="form-control" placeholder="Enter Age">
</div>
</div>
<table class="table table-striped">
<thead>
<tr>
<th v-repeat="column: columns">
<a href="#" v-on="click: sortBy(column)" v-class="active: sortKey == column">
{{ column | capitalize }}
</a>
</th>
</tr>
</thead>
<tbody>
<tr v-repeat="users | filterBy search | orderBy sortKey reverse">
<td>{{ name }}</td>
<td>{{ age }}</td>
</tr>
</tbody>
</table>
</div>
Vue :
new Vue({
el: '#demo',
data: {
sortKey: 'name',
reverse: false,
searchName: '',
searchOperator: '',
searchAge: '',
columns: ['name', 'age'],
newUser: {},
users: [
{ name: 'John', age: 50 },
{ name: 'Jane', age: 22 },
{ name: 'Paul', age: 34 }
]
},
methods: {
sortBy: function(sortKey) {
this.reverse = (this.sortKey == sortKey) ? !this.reverse : false;
this.sortKey = sortKey;
}
}
});
I'm currently struggling to implement the filter by age feature. Any suggestions on the best approach to achieve this?