15.列表排序.html 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. 关键字搜索:
  11. <input type="text" placeholder="请输入关键字" v-model="keywords">
  12. <br>
  13. <ul v-for="(item,index) in newList" :key="index">
  14. <li>{{index+1}}.我叫{{item.name}}--今年{{item.age}}了--我是{{item.sex}}孩</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. arr: [
  28. {
  29. name: "Lucy",
  30. age: 11,
  31. sex: "女",
  32. },
  33. {
  34. name: "LiLi",
  35. age: 31,
  36. sex: "男",
  37. },
  38. {
  39. name: "八戒",
  40. age: 21,
  41. sex: "男",
  42. },
  43. {
  44. name: "八卦",
  45. age: 23,
  46. sex: "男",
  47. },
  48. {
  49. name: "孙悟空",
  50. age: 27,
  51. sex: "男",
  52. },
  53. {
  54. name: "唐僧",
  55. age: 33,
  56. sex: "女",
  57. },
  58. ],
  59. personList:[]
  60. },
  61. computed: {
  62. newList() {
  63. this.personList =this.arr.filter((item => {
  64. return item.name.indexOf(this.keywords) !== -1;
  65. }));
  66. if(this.sortType) {
  67. this.personList.sort((a,b) => {
  68. return this.sortType == 1 ? a.age - b.age : b.age - a.age;
  69. })
  70. }
  71. return this.personList;
  72. }
  73. }
  74. })
  75. </script>
  76. </body>
  77. </html>