<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>列表排序</title>
<script src="../js/vue.js"></script>
<style>
table,tr,td,th{
border:1px solid black;
}
</style>
</head>
<body>
<div id="app">
<h1>{{msg}}</h1>
<input type="text" placeholder="输入关键字搜索" v-model="keyword"><br>
<button @click="type = 1">按照名字升序</button><br>
<button @click="type = 2">按照名字降序</button><br>
<button @click="type = 0">按照名字原始顺序</button><br>
<table>
<tr>
<th>序号</th>
<th>姓名</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<tr v-for="(vip, index) in filterVips" :key="vip.id">
<td>{{index+1}}</td>
<td>{{vip.name}}</td>
<td>{{vip.email}}</td>
<td><input type="checkbox"></td>
</tr>
</table>
</div>
<script>
const vm = new Vue({
el : '#app', 40. data : {
msg : '列表排序', 42. vips : [
{id:'100',name:'jack',email:'jack@123.com'}, 44. {id:'200',name:'lucy',email:'lucy@123.com'}, 45. {id:'300',name:'james',email:'james@123.com'}, 46. {id:'400',name:'lilei',email:'lilei@123.com'}, 47. ], 48. keyword : '', 49. type : 0
}, 51. computed : {
filterVips(){
// 筛选
let arr = this.vips.filter((vip) => {
return vip.name.indexOf(this.keyword) >= 0
})
// 根据排序类型进行排序
if(this.type){
arr.sort((v1, v2) => {
console.log('@')
return this.type == 1 ? v1.name.localeCompare(v2.name) : v2.name.localeCompare(v1.name)
})
}
// 返回
return arr
}
}
})
</script>
</body>
</html>