6.列表排序.html 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <input type="text" v-model="keyWords">
  11. <ul>
  12. <li v-for="(item,index) in newList" :key="index">
  13. {{item.name}}--{{item.age}}--{{item.sex}}
  14. </li>
  15. </ul>
  16. <button @click="sortType = 1">升序排列</button>
  17. <button @click="sortType = 2">降序排列</button>
  18. <button @click="sortType = 0">原来的顺序</button>
  19. </div>
  20. <script src="./vue.js"></script>
  21. <script>
  22. var app = new Vue({
  23. el:"#app",
  24. data:{
  25. sortType: 0,
  26. keyWords:"",
  27. person:[
  28. {
  29. name:"LiLi",
  30. age: 19,
  31. sex: '女'
  32. },
  33. {
  34. name:"Ming",
  35. age: 9,
  36. sex: '男'
  37. },
  38. {
  39. name:"Lucy",
  40. age: 99,
  41. sex: '女'
  42. },
  43. {
  44. name:"马冬梅",
  45. age: 33,
  46. sex: '女'
  47. },
  48. ],
  49. },
  50. computed:{
  51. newList() {
  52. const arr = this.person.filter((item) => {
  53. return item.name.indexOf(this.keyWords) !== -1;
  54. })
  55. // sort 升序 a-b 降序 b-a
  56. // sort(a,b) => a-b
  57. // arr.sort((a,b) =>{
  58. // return
  59. // })
  60. if(this.sortType) {
  61. arr.sort((a,b) => {
  62. return this.sortType === 1 ? a.age-b.age : b.age - a.age;
  63. })
  64. }
  65. return arr;
  66. }
  67. }
  68. }).$mount("#app")
  69. </script>
  70. </body>
  71. </html>