5_todolist.html 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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="seachVal">
  31. <button @click="search()">搜索</button>
  32. </div>
  33. <ul>
  34. <li v-for="(obj,index) in list" v-show="obj.isShow" :class="{active:obj.isChecked}">
  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. <button @click="del1()">删除选择</button>
  43. <span>总价{{total()}}</span>
  44. </div>
  45. </div>
  46. <script src="vue.js"></script>
  47. <script>
  48. new Vue({
  49. el:"#app",
  50. data:{
  51. list:[{
  52. name:'苹果',
  53. price:5,
  54. isShow: true,
  55. isChecked: false
  56. },{
  57. name:'香蕉',
  58. price:6,
  59. isShow: true,
  60. isChecked: false
  61. }],
  62. name:'',
  63. price:'',
  64. seachVal:''
  65. },
  66. methods:{
  67. add(){
  68. this.list.push({
  69. name: this.name,
  70. price: this.price,
  71. isShow: true,
  72. isChecked: false
  73. })
  74. this.name = ''
  75. this.price = ''
  76. },
  77. del(index){
  78. this.list.splice(index,1)
  79. },
  80. search(){
  81. this.list.forEach((obj)=>{
  82. if(obj.name.includes(this.seachVal)){
  83. obj.isShow = true
  84. } else {
  85. obj.isShow = false
  86. }
  87. })
  88. this.seachVal = ''
  89. },
  90. total(){
  91. var sum = 0;
  92. this.list.forEach(function(obj){
  93. if(obj.isChecked){
  94. sum += obj.price*1
  95. }
  96. })
  97. return sum
  98. },
  99. del1(){
  100. this.list = this.list.filter((obj)=>{
  101. return !obj.isChecked
  102. })
  103. console.log(this.list)
  104. }
  105. }
  106. })
  107. </script>
  108. </body>
  109. </html>