123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <div id="app">
- <input type="text" v-model="keyWords">
- <ul>
- <li v-for="(item,index) in newList" :key="index">
- {{item.name}}--{{item.age}}--{{item.sex}}
- </li>
- </ul>
- <button @click="sortType = 1">升序排列</button>
- <button @click="sortType = 2">降序排列</button>
- <button @click="sortType = 0">原来的顺序</button>
- </div>
- <script src="./vue.js"></script>
- <script>
- var app = new Vue({
- el:"#app",
- data:{
- sortType: 0,
- keyWords:"",
- person:[
- {
- name:"LiLi",
- age: 19,
- sex: '女'
- },
- {
- name:"Ming",
- age: 9,
- sex: '男'
- },
- {
- name:"Lucy",
- age: 99,
- sex: '女'
- },
- {
- name:"马冬梅",
- age: 33,
- sex: '女'
- },
- ],
- },
- computed:{
- newList() {
- const arr = this.person.filter((item) => {
- return item.name.indexOf(this.keyWords) !== -1;
- })
- // sort 升序 a-b 降序 b-a
- // sort(a,b) => a-b
- // arr.sort((a,b) =>{
- // return
- // })
- if(this.sortType) {
- arr.sort((a,b) => {
- return this.sortType === 1 ? a.age-b.age : b.age - a.age;
- })
- }
- return arr;
- }
- }
- }).$mount("#app")
- </script>
- </body>
- </html>
|