Browse Source

fix:day27

e 1 year ago
parent
commit
702256f2d2
1 changed files with 72 additions and 0 deletions
  1. 72 0
      day27/6.列表排序.html

+ 72 - 0
day27/6.列表排序.html

@@ -0,0 +1,72 @@
+<!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>