<!DOCTYPE html>
<html lang="en">
<head>
<script src="js/vue.js"></script>
<style>
</style>
</head>
<body>
<div id="app">
filter by namee : <input type="text" v-model="shuru"/> <br/>
<ul>
<li v-for="(p,index) in filterpersons" :key="index">
{{p.namee}}----{{p.age}}
</li>
</ul>
<button @click="paixu(1)">up</button>
<button @click="paixu(2)">down</button>
<button @click="paixu(0)">default</button>
</div>
</body>
<script type="text/javascript">
var vue = new Vue({
el: "#app",
data: {
persons: [{namee: "xuhaitao", age: 33}, {namee: "hunk", age: 45}, {namee: "xuhaihuan", age: 30}],
xuhao: ["A", "B", "C", "D"],
shuru: '',
zhi: 0
},
computed: {
filterpersons() {
var $this = this; // 相当于这种写法 const {persons,shuru}=this;
var filterP = this.persons.filter(function (item) {
return item.namee.indexOf($this.shuru) != -1;
});
if (this.zhi == 0) {
return filterP;
} else if (this.zhi == 1) {
return filterP.sort(function (p1, p2) {
return p1.age - p2.age;
})
} else if (this.zhi == 2) {
return filterP.sort(function (p1, p2) {
return p2.age - p1.age;
})
}
}
},
methods: {
paixu(value) {
this.zhi = value;
}
}
});
</script>
</html>
Vue 搜索和排序
最新推荐文章于 2023-07-11 11:29:50 发布