Blogger Information
Blog 20
fans 0
comment 1
visits 13152
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Vue 购物车页面计算功能的实现
zg的php学习
Original
449 people have browsed it

Vue 购物车页面计算功能的实现

  1. <template>
  2. <div>
  3. <table class="cart">
  4. <caption>
  5. 购物车
  6. </caption>
  7. <thead>
  8. <th></th>
  9. <th>编号</th>
  10. <th>商品名称</th>
  11. <th>价格</th>
  12. <th>数量</th>
  13. <th></th>
  14. </thead>
  15. <tbody>
  16. <tr v-for="(row, index) in cartlist" :key="index">
  17. <td><input type="checkbox" v-model="row.checkbox" /></td>
  18. <td>{{ row.id }}</td>
  19. <td>{{ row.name }}</td>
  20. <td>{{ row.price.toFixed(2) }}</td>
  21. <td>
  22. <button @click="row.count--" :disabled="row.count <= 1">-</button>
  23. {{ row.count }}
  24. <button @click="row.count++">+</button>
  25. </td>
  26. <td><a href="#" @click.prevent="del(index)">删除</a></td>
  27. </tr>
  28. </tbody>
  29. <tfoot>
  30. <tr>
  31. <td colspan="3">总价</td>
  32. <td colspan="3">¥{{ totalPrice }}</td>
  33. </tr>
  34. </tfoot>
  35. </table>
  36. </div>
  37. </template>
  38. <script>
  39. const cartArr = [
  40. { id: 1, checkbox: false, name: "《细说PHP》", price: 10, count: 1 },
  41. { id: 2, checkbox: true, name: "《细说网页制作》", price: 10, count: 1 },
  42. {
  43. id: 3,
  44. checkbox: true,
  45. name: "《细说JavaScript 语言》",
  46. price: 10,
  47. count: 1,
  48. },
  49. { id: 4, checkbox: true, name: "《细说DOM 和BOM》", price: 10, count: 1 },
  50. {
  51. id: 5,
  52. checkbox: false,
  53. name: "《细说Ajax 与jQuery》",
  54. price: 10,
  55. count: 1,
  56. },
  57. { id: 6, checkbox: true, name: "《细说HTML5 高级API》", price: 10, count: 1 },
  58. ];
  59. export default {
  60. name: "app",
  61. data() {
  62. return {
  63. cartlist: cartArr,
  64. };
  65. },
  66. computed: {
  67. totalPrice: {
  68. get() {
  69. return this.cartlist
  70. .filter((row) => row.checkbox)
  71. .reduce((p, c) => {
  72. return (p += c.price * c.count);
  73. }, 0)
  74. .toFixed(2);
  75. },
  76. },
  77. },
  78. methods: {
  79. del(index) {
  80. this.cartlist.splice(index, 1);
  81. },
  82. },
  83. };
  84. </script>
  85. <style>
  86. .cart {
  87. width: 500px;
  88. border-collapse: collapse;
  89. text-align: center;
  90. }
  91. .cart td,
  92. .cart th {
  93. border: 1px solid black;
  94. padding: 2px 3px;
  95. }
  96. .cart tbody tr:hover {
  97. background-color: #e3e3e3;
  98. }
  99. </style>

购物车

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post