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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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="请输入商品名称"
  35. aria-label="First name">
  36. </div>
  37. <div class="col-4">
  38. <button @click="searchFun" type="button" class="form-control btn btn-primary">搜索</button>
  39. </div>
  40. </div>
  41. <div class="input-group mb-3 add-box">
  42. <input v-model="goodsName" type="text" class="form-control" placeholder="请输入商品名称"
  43. aria-label="Recipient's username" aria-describedby="button-addon2">
  44. <input v-model="goodsPrice" type="text" class="form-control" placeholder="请输入商品价格"
  45. aria-label="Recipient's username" aria-describedby="button-addon2">
  46. <button @click="addFun" class="btn btn-outline-secondary" type="button" id="button-addon2">添加</button>
  47. </div>
  48. <table class="table">
  49. <thead>
  50. <tr>
  51. <th scope="col">#</th>
  52. <th scope="col">#</th>
  53. <th scope="col">商品名称</th>
  54. <th scope="col">商品价格</th>
  55. <th scope="col">操作</th>
  56. </tr>
  57. </thead>
  58. <tbody>
  59. <!-- <tr @click="checkLine(item.id)" :class="{'active':item.isCheck}" v-for="(item,index) in tableData" > -->
  60. <tr @click="item.isCheck = !item.isCheck" :class="{'active':item.isCheck}"
  61. v-for="(item,index) in tableData">
  62. <th scope="row">{{index+1}}</th>
  63. <td>
  64. <input type="checkbox" :checked="item.isCheck">
  65. </td>
  66. <td>{{item.name}}</td>
  67. <td>{{item.price}}</td>
  68. <th>
  69. <button @click.stop="delSingle(item.id)" type="button"
  70. class="btn btn-primary btn-sm">删除</button>
  71. </th>
  72. </tr>
  73. <tr>
  74. <th scope="row" colspan="3">总价</th>
  75. <td>{{totalPrice}}</td>
  76. <td>
  77. <button @click="delCheck" type="button" class="btn btn-primary btn-sm">删除选中</button>
  78. </td>
  79. </tr>
  80. </tbody>
  81. </table>
  82. </div>
  83. </div>
  84. <script src="./js/vue.js"></script>
  85. <script>
  86. let _data = [
  87. {
  88. id: 1001,
  89. name: "衣服",
  90. price: 100,
  91. isCheck: false
  92. },
  93. {
  94. id: 1002,
  95. name: "裤子",
  96. price: 200,
  97. isCheck: false
  98. },
  99. {
  100. id: 1003,
  101. name: "帽子",
  102. price: 50,
  103. isCheck: false
  104. },
  105. {
  106. id: 1004,
  107. name: "鞋",
  108. price: 300,
  109. isCheck: false
  110. }
  111. ]
  112. new Vue({
  113. el: '#app',
  114. data: {
  115. tableData: _data,
  116. searchName: "",
  117. goodsName: "",
  118. goodsPrice: ""
  119. },
  120. computed: {
  121. // 计算选中商品的总价
  122. totalPrice() {
  123. let total = 0;
  124. // 1. 先筛选出选中的商品
  125. this.tableData.forEach((item) => {
  126. if (item.isCheck) {
  127. total += item.price;
  128. }
  129. });
  130. // 2. 返回总价
  131. return total;
  132. }
  133. },
  134. methods: {
  135. // 删除选中商品
  136. delCheck(){
  137. let newData = this.tableData.filter((item) => {
  138. return !item.isCheck
  139. })
  140. this.tableData = newData;
  141. },
  142. //删除单个商品
  143. delSingle(id) {
  144. let newData = this.tableData.filter((item) => {
  145. return item.id != id;
  146. })
  147. this.tableData = newData;
  148. },
  149. // 搜索
  150. searchFun() {
  151. // 过滤数组中符合条件的元素
  152. let newData = _data.filter((item) => {
  153. if (item.name.indexOf(this.searchName) != -1) {
  154. return true
  155. }
  156. })
  157. this.tableData = newData;
  158. },
  159. // 添加商品
  160. addFun() {
  161. // 1. 先判断商品名称和商品价格是否为空
  162. if (this.goodsName == "" || this.goodsPrice == "") {
  163. console.log("请输入商品名称和商品价格")
  164. } else {
  165. let newGood = {
  166. id: _data[_data.length - 1].id + 1,
  167. name: this.goodsName,
  168. price: this.goodsPrice,
  169. isCheck: false
  170. }
  171. // 2. 添加商品
  172. this.tableData.push(newGood)
  173. // 3. 清空输入框
  174. this.goodsName = ""
  175. this.goodsPrice = ""
  176. }
  177. },
  178. // 单行选中
  179. checkLine(id) {
  180. console.log(id);
  181. // 根据id值进行匹配修改isCheck
  182. let newData = this.tableData.map((item) => {
  183. if (item.id == id) {
  184. item.isCheck = !item.isCheck
  185. }
  186. return item;
  187. })
  188. this.tableData = newData;
  189. }
  190. }
  191. })
  192. </script>
  193. </body>
  194. </html>