5_todoList.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #app {
  10. width: 500px;
  11. }
  12. .close {
  13. float: right;
  14. }
  15. .active {
  16. background: red;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="app">
  22. <h2>todoList</h2>
  23. <hr>
  24. <div>
  25. 名称:<input type="text" v-model="name">
  26. 价格: <input type="text" v-model="price">
  27. <button @click="add">提交</button>
  28. </div>
  29. <div>
  30. <input type="text" v-model="searchVal">
  31. <button @click="search">搜索</button>
  32. </div>
  33. <ul>
  34. <li v-for="(obj,index) in list" v-show="obj.isShow" :class="{active: obj.isCheck}">
  35. <input type="checkbox" v-model="obj.isCheck">
  36. <span>{{obj.name}}</span>
  37. <span>{{obj.price}}</span>
  38. <span class="close" @click="del(index)">[X]</span>
  39. </li>
  40. </ul>
  41. <div>
  42. <button>删除选中</button>
  43. <span>总价:{{total()}}</span>
  44. </div>
  45. </div>
  46. <script src="vue.js"></script>
  47. <script>
  48. var app = new Vue({
  49. el: '#app',
  50. data: {
  51. list: [{
  52. name: '苹果',
  53. price: 5,
  54. isShow: true,
  55. isCheck: false
  56. }, {
  57. name: '香蕉',
  58. price: 8,
  59. isShow: true,
  60. isCheck: false
  61. }],
  62. name: '',
  63. price: '',
  64. searchVal: ''
  65. },
  66. methods: {
  67. add() {
  68. this.list.push({
  69. name: this.name,
  70. price: this.price,
  71. isShow: true,
  72. isCheck: false
  73. })
  74. this.name = ''
  75. this.price = ''
  76. },
  77. search() {
  78. this.list.forEach((obj) => {
  79. if (obj.name.includes(this.searchVal)) {
  80. obj.isShow = true
  81. } else {
  82. obj.isShow = false
  83. }
  84. })
  85. this.searchVal = ''
  86. },
  87. del(index) {
  88. this.list.splice(index, 1)
  89. },
  90. total() {
  91. var num = 0
  92. this.list.forEach((obj) => {
  93. if (obj.isCheck) {
  94. num += obj.price * 1
  95. }
  96. })
  97. return num
  98. }
  99. }
  100. })
  101. </script>
  102. </body>
  103. </html>