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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 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. searchInp: "",
  89. dataList: [
  90. {
  91. id: 1001,
  92. name: "衣服",
  93. price: 100,
  94. isCheck: false
  95. },
  96. {
  97. id: 1002,
  98. name: "裤子",
  99. price: 200,
  100. isCheck: false
  101. },
  102. {
  103. id: 1003,
  104. name: "帽子",
  105. price: 50,
  106. isCheck: false
  107. },
  108. {
  109. id: 1004,
  110. name: "鞋",
  111. price: 300,
  112. isCheck: false
  113. }
  114. ]
  115. },
  116. methods: {
  117. // 单行选中
  118. checkLine(id) {
  119. this.dataList.map((val) => {
  120. if (val.id == id) {
  121. val.isCheck = !val.isCheck;
  122. }
  123. })
  124. },
  125. // 单行删除
  126. delFun(id){
  127. let newDataList = this.dataList.filter((val)=>{
  128. if(val.id != id){
  129. return true
  130. }
  131. })
  132. this.dataList = newDataList;
  133. },
  134. // 删除选中
  135. delCheck(){
  136. let newDataList = this.dataList.filter((val)=>{
  137. if(!val.isCheck){
  138. return true
  139. }
  140. })
  141. this.dataList = newDataList;
  142. },
  143. // 搜索
  144. searchFun(){
  145. let searchVal = this.searchInp;
  146. let newDataList = this.dataList.filter((val)=>{
  147. if(val.name.includes(searchVal)){
  148. return true
  149. }
  150. });
  151. this.dataList = newDataList;
  152. }
  153. },
  154. computed: {
  155. sum() {
  156. let sumVal = 0;
  157. this.dataList.forEach((val)=>{
  158. if(val.isCheck){
  159. sumVal += val.price;
  160. }
  161. });
  162. return sumVal
  163. }
  164. }
  165. })
  166. </script>
  167. </body>
  168. </html>