练习4_商品列表.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet"
  8. crossorigin="anonymous">
  9. <style>
  10. .navbar-brand {
  11. color: white;
  12. }
  13. .search-box {
  14. width: 400px;
  15. }
  16. .add-box {
  17. width: 600px;
  18. }
  19. .active{
  20. background-color: #ddd;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="app">
  26. <div class="container">
  27. <nav class="navbar bg-primary mt-3" data-bs-theme="dark">
  28. <div class="container-fluid">
  29. <span class="navbar-brand mb-0 h1">商品管理</span>
  30. </div>
  31. </nav>
  32. <div class="row search-box my-3">
  33. <div class="col-8">
  34. <input v-model="searchName" type="text" class="form-control" placeholder="请输入商品名称" aria-label="First name">
  35. </div>
  36. <div class="col-4">
  37. <button @click="searchFun" type="button" class="form-control btn btn-primary">搜索</button>
  38. </div>
  39. </div>
  40. <div class="input-group mb-3 add-box">
  41. <input v-model="goodsName" type="text" class="form-control" placeholder="请输入商品名称" aria-label="Recipient's username"
  42. aria-describedby="button-addon2">
  43. <input v-model="goodsPrice" type="text" class="form-control" placeholder="请输入商品价格" aria-label="Recipient's username"
  44. aria-describedby="button-addon2">
  45. <button @click="addFun" class="btn btn-outline-secondary" type="button" id="button-addon2">添加</button>
  46. </div>
  47. <table class="table">
  48. <thead>
  49. <tr>
  50. <th scope="col">#</th>
  51. <th scope="col">#</th>
  52. <th scope="col">商品名称</th>
  53. <th scope="col">商品价格</th>
  54. <th scope="col">操作</th>
  55. </tr>
  56. </thead>
  57. <tbody>
  58. <!-- <tr @click="checkLine(item.id)" :class="{'active':item.isCheck}" v-for="(item,index) in tableData" > -->
  59. <tr @click="item.isCheck = !item.isCheck" :class="{'active':item.isCheck}" v-for="(item,index) in tableData" >
  60. <th scope="row">{{index+1}}</th>
  61. <td>
  62. <input type="checkbox" :checked="item.isCheck">
  63. </td>
  64. <td>{{item.name}}</td>
  65. <td>{{item.price}}</td>
  66. <th>
  67. <button type="button" class="btn btn-primary btn-sm">删除</button>
  68. </th>
  69. </tr>
  70. <th scope="row" colspan="3">总价</th>
  71. <td>{{totalPrice}}</td>
  72. <td>
  73. <button type="button" class="btn btn-primary btn-sm">删除选中</button>
  74. </td>
  75. </tr>
  76. </tbody>
  77. </table>
  78. </div>
  79. </div>
  80. <script src="./js/vue.js"></script>
  81. <script>
  82. let _data = [
  83. {
  84. id: 1001,
  85. name: "衣服",
  86. price: 100,
  87. isCheck: false
  88. },
  89. {
  90. id: 1002,
  91. name: "裤子",
  92. price: 200,
  93. isCheck: false
  94. },
  95. {
  96. id: 1003,
  97. name: "帽子",
  98. price: 50,
  99. isCheck: false
  100. },
  101. {
  102. id: 1004,
  103. name: "鞋",
  104. price: 300,
  105. isCheck: false
  106. }
  107. ]
  108. new Vue({
  109. el: '#app',
  110. data: {
  111. tableData:_data,
  112. searchName:"",
  113. goodsName:"",
  114. goodsPrice:""
  115. },
  116. computed:{
  117. // 计算选中商品的总价
  118. totalPrice(){
  119. let total = 0;
  120. // 1. 先筛选出选中的商品
  121. this.tableData.forEach((item) => {
  122. if(item.isCheck){
  123. total += item.price;
  124. }
  125. });
  126. // 2. 返回总价
  127. return total;
  128. }
  129. },
  130. methods:{
  131. // 搜索
  132. searchFun(){
  133. // 过滤数组中符合条件的元素
  134. let newData = _data.filter((item)=>{
  135. if(item.name.indexOf(this.searchName)!=-1){
  136. return true
  137. }
  138. })
  139. this.tableData = newData;
  140. },
  141. // 添加商品
  142. addFun(){
  143. // 1. 先判断商品名称和商品价格是否为空
  144. if(this.goodsName=="" || this.goodsPrice==""){
  145. console.log("请输入商品名称和商品价格")
  146. }else{
  147. let newGood = {
  148. id:_data[_data.length-1].id+1,
  149. name:this.goodsName,
  150. price:this.goodsPrice,
  151. isCheck:false
  152. }
  153. // 2. 添加商品
  154. this.tableData.push(newGood)
  155. // 3. 清空输入框
  156. this.goodsName=""
  157. this.goodsPrice=""
  158. }
  159. },
  160. // 单行选中
  161. checkLine(id){
  162. console.log(id);
  163. // 根据id值进行匹配修改isCheck
  164. let newData = this.tableData.map((item)=>{
  165. if(item.id == id){
  166. item.isCheck = !item.isCheck
  167. }
  168. return item;
  169. })
  170. this.tableData = newData;
  171. }
  172. }
  173. })
  174. </script>
  175. </body>
  176. </html>