表有两列Name
,Age
。按名称搜索作品。当您键入用户名时,表格将缩减为特定的用户名。
但我希望使用比较运算符<
或>
.
html
<div id="demo" class="container">
<div class="row">
<div class="col-md-6">
<input v-model="search" class="form-control" placeholder="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="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>
Run Code Online (Sandbox Code Playgroud)
视图:
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;
}
}
});
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最佳方法是什么?我试过了,但似乎没有任何效果。
第一件事是:v-repeat
已弃用。所以你应该改用v-for
。
<tr v-for="user in filteredPersons">
<td>{{ user.name }}</td>
<td>{{ user.age }}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
filteredPersons
是返回数组的计算函数的名称:
computed: {
filteredPersons: function () {
return this.users
.filter(this.filterByName)
.filter(this.filterByAge)
.sort(this.orderBy);
}
}
Run Code Online (Sandbox Code Playgroud)
它users
通过组合两个过滤器函数和一个比较器函数来对数组进行过滤和排序:
methods: {
filterByName : function(user) {
// no search, don't filter :
if (this.searchName.length === 0) {
return true;
}
return (user.name.toLowerCase().indexOf(this.searchName.toLowerCase()) > -1);
},
filterByAge : function (user) {
// no operator selected or no age typed, don't filter :
if (this.searchOperator.length === 0 || this.age.length === 0) {
return true;
}
if (this.searchOperator === '>') {
return (user.age > this.age);
} else if (this.searchOperator === '<') {
return (user.age < this.age);
}
},
orderBy : function (userA, userB) {
let condition = (userA[this.sortKey] > userB[this.sortKey]);
if (this.reverse) {
return !condition;
} else {
return condition;
}
}
},
Run Code Online (Sandbox Code Playgroud)
工作片段:
<tr v-for="user in filteredPersons">
<td>{{ user.name }}</td>
<td>{{ user.age }}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
computed: {
filteredPersons: function () {
return this.users
.filter(this.filterByName)
.filter(this.filterByAge)
.sort(this.orderBy);
}
}
Run Code Online (Sandbox Code Playgroud)
methods: {
filterByName : function(user) {
// no search, don't filter :
if (this.searchName.length === 0) {
return true;
}
return (user.name.toLowerCase().indexOf(this.searchName.toLowerCase()) > -1);
},
filterByAge : function (user) {
// no operator selected or no age typed, don't filter :
if (this.searchOperator.length === 0 || this.age.length === 0) {
return true;
}
if (this.searchOperator === '>') {
return (user.age > this.age);
} else if (this.searchOperator === '<') {
return (user.age < this.age);
}
},
orderBy : function (userA, userB) {
let condition = (userA[this.sortKey] > userB[this.sortKey]);
if (this.reverse) {
return !condition;
} else {
return condition;
}
}
},
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5291 次 |
最近记录: |