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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 type="text" class="form-control" placeholder="请输入搜索名称">
  34. </div>
  35. <div class="col-10">
  36. <button type="button" class="btn btn-primary">搜索</button>
  37. </div>
  38. </div>
  39. <!-- 添加商品 -->
  40. <div class="add-goods input-group mt-3">
  41. <input type="text" class="form-control" placeholder="商品名称" aria-label="Recipient's username"
  42. aria-describedby="button-addon2">
  43. <input type="text" class="form-control" placeholder="商品价格" aria-label="Recipient's username"
  44. aria-describedby="button-addon2">
  45. <button 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. dataList: [
  89. {
  90. id: 1001,
  91. name: "衣服",
  92. price: 100,
  93. isCheck: false
  94. },
  95. {
  96. id: 1002,
  97. name: "裤子",
  98. price: 200,
  99. isCheck: false
  100. },
  101. {
  102. id: 1003,
  103. name: "帽子",
  104. price: 50,
  105. isCheck: false
  106. },
  107. {
  108. id: 1004,
  109. name: "鞋",
  110. price: 300,
  111. isCheck: false
  112. }
  113. ]
  114. },
  115. methods: {
  116. // 单行选中
  117. checkLine(id) {
  118. this.dataList.map((val) => {
  119. if (val.id == id) {
  120. val.isCheck = !val.isCheck;
  121. }
  122. })
  123. },
  124. // 单行删除
  125. delFun(id){
  126. let newDataList = this.dataList.filter((val)=>{
  127. if(val.id != id){
  128. return true
  129. }
  130. })
  131. this.dataList = newDataList;
  132. },
  133. // 删除选中
  134. delCheck(){
  135. let newDataList = this.dataList.filter((val)=>{
  136. if(!val.isCheck){
  137. return true
  138. }
  139. })
  140. this.dataList = newDataList;
  141. }
  142. },
  143. computed: {
  144. sum() {
  145. let sumVal = 0;
  146. this.dataList.forEach((val)=>{
  147. if(val.isCheck){
  148. sumVal += val.price;
  149. }
  150. });
  151. return sumVal
  152. }
  153. }
  154. })
  155. </script>
  156. </body>
  157. </html>