11_todolist.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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" :class="{active: obj.isChecked}" v-show="obj.isShow">
  35. <input type="checkbox" v-model="obj.isChecked">
  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. <span>总价:{{total()}}</span>
  43. </div>
  44. </div>
  45. <script src="./vue.js"></script>
  46. <script>
  47. var app = new Vue({
  48. el: '#app',
  49. data: {
  50. list: [{
  51. name: '苹果',
  52. price: 5,
  53. isChecked: false,
  54. isShow: true
  55. }, {
  56. name: '香蕉',
  57. price: 8,
  58. isChecked: false,
  59. isShow: true
  60. }],
  61. name: '',
  62. price: '',
  63. searchVal: ''
  64. },
  65. methods: {
  66. add() {
  67. this.list.push({
  68. name: this.name,
  69. price: this.price,
  70. isChecked: false,
  71. isShow: true
  72. })
  73. this.name = ''
  74. this.price = ''
  75. },
  76. total(){
  77. var sum = 0
  78. this.list.forEach((obj)=>{
  79. if(obj.isChecked){
  80. sum += obj.price * 1
  81. }
  82. })
  83. return sum
  84. },
  85. search(){
  86. this.list.forEach((obj)=>{
  87. if(obj.name.includes(this.searchVal)){
  88. obj.isShow = true
  89. } else {
  90. obj.isShow = false
  91. }
  92. })
  93. this.searchVal = ''
  94. },
  95. del(index){
  96. this.list.splice(index,1)
  97. }
  98. }
  99. })
  100. </script>
  101. </body>
  102. </html>