vue04.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模板</title>
  6. <!-- 引入文件 -->
  7. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  8. <style>
  9. .active {
  10. height: 200px;
  11. width: 200px;
  12. border: 1px solid red;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="app">
  18. <h1>图书列表</h1>
  19. <table width="80%" cellspacing="10" cellpadding="0" >
  20. <tr>
  21. <th>编号</th>
  22. <th>名称</th>
  23. <th>价格</th>
  24. <th>作者</th>
  25. <th>类型</th> <!-- 类型 下拉框 -->
  26. <th>下架</th> <!-- 下架 单选框 -->
  27. <th>标签</th> <!-- 标签 多选框 -->
  28. <th>介绍</th> <!-- 介绍 文本域 -->
  29. <th>数量</th>
  30. <th>操作</th>
  31. </tr>
  32. <tr v-for="(item, index) in books " :key="item.id" align="center" >
  33. <td>{{item.id}}</td>
  34. <td>{{item.name}}</td>
  35. <td>{{item.price}}</td>
  36. <td>{{item.author}}</td>
  37. <td>{{item.type}}</td>
  38. <td>{{item.flag}}</td>
  39. <td>{{item.labelstr}}</td>
  40. <td>{{item.desc}}</td>
  41. <td>
  42. <button @click="subBookCount(index)" >-</button>
  43. {{item.count}}
  44. <button @click="item.count++" >+</button>
  45. </td>
  46. <td>
  47. <button @click="delBooks(index)" >删除</button>
  48. </td>
  49. </tr>
  50. </table>
  51. <h3>总价格:{{totalPrice}}</h3>
  52. <hr>
  53. 编号 <input v-model="bookForm.id" type="text" placeholder="id" >
  54. <br>
  55. 书名:<input v-model="bookForm.name" type="text" placeholder="书名" >
  56. <br>
  57. 价格: <input v-model="bookForm.price" type="text" placeholder="价格" >
  58. <br>
  59. 作者: <input v-model="bookForm.author" type="text" placeholder="作者" >
  60. <br>
  61. 类型:
  62. <select v-model="bookForm.type" >
  63. <option value="0" >---请选择---</option>
  64. <option value="玄幻" >玄幻</option>
  65. <option value="武侠" >武侠</option>
  66. <option value="修仙" >修仙</option>
  67. </select>
  68. <br>
  69. 是否下架:
  70. <input type="radio" value="是" v-model="bookForm.flag" name="flag" > 是
  71. <input type="radio" value="否" v-model="bookForm.flag" name="flag" > 否
  72. <br>
  73. 标签:
  74. <input type="checkbox" value="热血" v-model="bookForm.label" name="label" > 热血
  75. <input type="checkbox" value="西方" v-model="bookForm.label" name="label" > 西方
  76. <input type="checkbox" value="魔幻" v-model="bookForm.label" name="label" > 魔幻
  77. <input type="checkbox" value="现代" v-model="bookForm.label" name="label" > 现代
  78. <input type="checkbox" value="神话" v-model="bookForm.label" name="label" > 神话
  79. <br>
  80. 介绍
  81. <textarea v-model="bookForm.desc" cols="30" rows="10" ></textarea>
  82. <hr>
  83. <button @click="addBook" > 添加 </button>
  84. </div>
  85. </body>
  86. <script>
  87. const { createApp, ref , computed ,watch ,onMounted ,onUpdated } = Vue;
  88. createApp({
  89. setup() {
  90. //数组
  91. let books = ref([
  92. {id: 1, name: '神墓', price: 129.0, author: '辰东',type: '玄幻',flag:'是', labelstr:"西方, 魔神", desc:"神魔陵园除了安葬着人类历代的最强者", count:1},
  93. {id: 2, name: '斗破', price: 199.0, author: '土豆',type: '玄幻',flag:'是', labelstr:"西方, 魔神" ,desc:"神魔陵园除了安葬着人类历代的最强者", count:1},
  94. {id: 3, name: '大主宰', price: 119, author: '土豆',type: '玄幻',flag:'是', labelstr:"西方, 魔神" , desc:"神魔陵园除了安葬着人类历代的最强者", count:1},
  95. ])
  96. let idAuto = computed( ()=>{
  97. return books.value.length+1;
  98. })
  99. //form 数据
  100. let bookDefaultForm = {
  101. id: idAuto,
  102. name: '',
  103. price: 0,
  104. author: '',
  105. type: '0',
  106. flag: '是',
  107. label: ['西方'],
  108. desc: '',
  109. count: 1 //编号
  110. }
  111. //表单
  112. let bookForm = ref({...bookDefaultForm})
  113. // 一个计算属性 ref 总价格
  114. const totalPrice = computed(() => {
  115. let sumPrice = 0;
  116. for (let i = 0; i < books.value.length; i++) {
  117. sumPrice += (parseInt(books.value[i].count) * parseInt(books.value[i].price))
  118. }
  119. return sumPrice
  120. })
  121. //添加
  122. function addBook(){
  123. //处理标签 展示string
  124. //join 把数组 通过逗号 拼接成字符串
  125. bookForm.value.labelstr = bookForm.value.label.join(",")
  126. //bookForm 添加到数组
  127. books.value.push({...bookForm.value})
  128. //清空
  129. bookForm.value = {...bookDefaultForm}
  130. }
  131. //删除
  132. function delBook(index){
  133. books.value.splice(index, 1)
  134. }
  135. function subBookCount(index){
  136. //console.log(index)
  137. if (books.value[index].count == 0){
  138. books.value[index].count = 0;
  139. }else{
  140. books.value[index].count--;
  141. }
  142. }
  143. function delBooks(index){
  144. //splice 实现替换 删除 增加
  145. let book = {id: 6, name: '灵龙', price: 129, author: '一画开天',count:1}
  146. // 删除
  147. books.value.splice(index,1)
  148. }
  149. return {
  150. books,subBookCount,totalPrice,delBooks,bookForm,addBook
  151. };
  152. }
  153. }).mount('#app'); //createApp创建对象 挂在到app
  154. </script>
  155. </html>