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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 v-for="(item,index) in tableData" >
  59. <th scope="row">{{index+1}}</th>
  60. <td>
  61. <input type="checkbox">
  62. </td>
  63. <td>{{item.name}}</td>
  64. <td>{{item.price}}</td>
  65. <th>
  66. <button type="button" class="btn btn-primary btn-sm">删除</button>
  67. </th>
  68. </tr>
  69. <th scope="row" colspan="3">总价</th>
  70. <td>0</td>
  71. <td>
  72. <button type="button" class="btn btn-primary btn-sm">删除选中</button>
  73. </td>
  74. </tr>
  75. </tbody>
  76. </table>
  77. </div>
  78. </div>
  79. <script src="./js/vue.js"></script>
  80. <script>
  81. let _data = [
  82. {
  83. id: 1001,
  84. name: "衣服",
  85. price: 100,
  86. isCheck: false
  87. },
  88. {
  89. id: 1002,
  90. name: "裤子",
  91. price: 200,
  92. isCheck: false
  93. },
  94. {
  95. id: 1003,
  96. name: "帽子",
  97. price: 50,
  98. isCheck: false
  99. },
  100. {
  101. id: 1004,
  102. name: "鞋",
  103. price: 300,
  104. isCheck: false
  105. }
  106. ]
  107. new Vue({
  108. el: '#app',
  109. data: {
  110. tableData:_data,
  111. searchName:"",
  112. goodsName:"",
  113. goodsPrice:""
  114. },
  115. methods:{
  116. // 搜索
  117. searchFun(){
  118. // 过滤数组中符合条件的元素
  119. let newData = _data.filter((item)=>{
  120. if(item.name.indexOf(this.searchName)!=-1){
  121. return true
  122. }
  123. })
  124. this.tableData = newData;
  125. },
  126. // 添加商品
  127. addFun(){
  128. // 1. 先判断商品名称和商品价格是否为空
  129. if(this.goodsName=="" || this.goodsPrice==""){
  130. console.log("请输入商品名称和商品价格")
  131. }else{
  132. let newGood = {
  133. id:_data[_data.length-1].id+1,
  134. name:this.goodsName,
  135. price:this.goodsPrice,
  136. isCheck:false
  137. }
  138. // 2. 添加商品
  139. this.tableData.push(newGood)
  140. // 3. 清空输入框
  141. this.goodsName=""
  142. this.goodsPrice=""
  143. }
  144. }
  145. }
  146. })
  147. </script>
  148. </body>
  149. </html>