练习4_列表管理.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. <script src="./js/vue.js"></script>
  8. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
  9. <style>
  10. .container {
  11. padding: 20px;
  12. }
  13. .navbar {
  14. margin-bottom: 20px;
  15. }
  16. .add-goods {
  17. width: 400px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="app">
  23. <div class="container">
  24. <!-- 顶部导航 -->
  25. <nav class="navbar bg-primary" data-bs-theme="dark">
  26. <div class="container-fluid">
  27. <span class="navbar-brand mb-0 h1">商品列表管理</span>
  28. </div>
  29. </nav>
  30. <!-- 搜索框 -->
  31. <div class="row">
  32. <div class="col-2">
  33. <input v-model="searchInp" type="text" class="form-control" placeholder="请输入搜索名称">
  34. </div>
  35. <div class="col-10">
  36. <button @click="searchFun" type="button" class="btn btn-primary">搜索</button>
  37. </div>
  38. </div>
  39. <!-- 添加商品 -->
  40. <div class="add-goods input-group mt-3">
  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.number="goodsPrice" type="text" class="form-control" placeholder="商品价格" aria-label="Recipient's username"
  44. aria-describedby="button-addon2">
  45. <button @click="addGoods" class="btn btn-outline-secondary" type="button" id="button-addon2">添加商品</button>
  46. </div>
  47. <!-- 表格区域 -->
  48. <div class="table-content">
  49. <table class="table">
  50. <thead>
  51. <tr>
  52. <th scope="col">#</th>
  53. <th scope="col">#</th>
  54. <th scope="col">商品名称</th>
  55. <th scope="col">商品价格</th>
  56. <th scope="col">操作</th>
  57. </tr>
  58. </thead>
  59. <tbody class="table-group-divider">
  60. <tr @click="checkLine(item.id)" :class="{'table-active':item.isCheck}"
  61. v-for="(item,index) in dataList" :key="item.id">
  62. <th scope="row">{{index+1}}</th>
  63. <td>
  64. <input type="checkbox" v-bind:checked="item.isCheck">
  65. </td>
  66. <td>{{item.name}}</td>
  67. <td>{{item.price}}</td>
  68. <td>
  69. <button @click.stop="delFun(item.id)" type="button" class="btn btn-primary btn-sm">删除</button>
  70. </td>
  71. </tr>
  72. <tr>
  73. <th colspan="3">总价</th>
  74. <td>{{sum}}</td>
  75. <td>
  76. <button @click="delCheck" type="button" class="btn btn-primary btn-sm">删除选中</button>
  77. </td>
  78. </tr>
  79. </tbody>
  80. </table>
  81. </div>
  82. </div>
  83. </div>
  84. <script>
  85. let app = new Vue({
  86. el: "#app",
  87. data: {
  88. goodsName:"",
  89. goodsPrice:"",
  90. searchInp: "",
  91. dataList: [
  92. {
  93. id: 1001,
  94. name: "衣服",
  95. price: 100,
  96. isCheck: false
  97. },
  98. {
  99. id: 1002,
  100. name: "裤子",
  101. price: 200,
  102. isCheck: false
  103. },
  104. {
  105. id: 1003,
  106. name: "帽子",
  107. price: 50,
  108. isCheck: false
  109. },
  110. {
  111. id: 1004,
  112. name: "鞋",
  113. price: 300,
  114. isCheck: false
  115. }
  116. ]
  117. },
  118. methods: {
  119. // 添加商品
  120. addGoods(){
  121. if(this.goodsName && this.goodsPrice){
  122. let newId = 0;
  123. // this.dataList.forEach((val)=>{
  124. // if(val.id > newId){
  125. // newId = val.id;
  126. // }
  127. // })
  128. // Math.max(1,2,3,4)
  129. // console.log(newId);
  130. let idList = this.dataList.map((val)=>{
  131. return val.id;
  132. })
  133. // console.log(Math.max(...idList));
  134. newId = Math.max(...this.dataList.map(val => val.id))+1;
  135. console.log(newId);
  136. let newGoods = {
  137. id:newId,
  138. name:this.goodsName,
  139. price:this.goodsPrice,
  140. isCheck:false
  141. }
  142. this.dataList.push(newGoods);
  143. }else{
  144. alert("请输入商品名称和价格");
  145. }
  146. },
  147. // 单行选中
  148. checkLine(id) {
  149. this.dataList.map((val) => {
  150. if (val.id == id) {
  151. val.isCheck = !val.isCheck;
  152. }
  153. })
  154. },
  155. // 单行删除
  156. delFun(id){
  157. let newDataList = this.dataList.filter((val)=>{
  158. if(val.id != id){
  159. return true
  160. }
  161. })
  162. this.dataList = newDataList;
  163. },
  164. // 删除选中
  165. delCheck(){
  166. let newDataList = this.dataList.filter((val)=>{
  167. if(!val.isCheck){
  168. return true
  169. }
  170. })
  171. this.dataList = newDataList;
  172. },
  173. // 搜索
  174. searchFun(){
  175. let searchVal = this.searchInp;
  176. let newDataList = this.dataList.filter((val)=>{
  177. if(val.name.includes(searchVal)){
  178. return true
  179. }
  180. });
  181. this.dataList = newDataList;
  182. }
  183. },
  184. computed: {
  185. sum() {
  186. let sumVal = 0;
  187. this.dataList.forEach((val)=>{
  188. if(val.isCheck){
  189. sumVal += val.price;
  190. }
  191. });
  192. return sumVal
  193. }
  194. }
  195. })
  196. </script>
  197. </body>
  198. </html>