1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <link rel="stylesheet" href="bootstrap.css">
    7. </head>
    8. <body>
    9. <div id="app">
    10. <div class="container">
    11. <div class="row">
    12. <h2 class="text-danger">购物车</h2>
    13. <table class="table table-bordered">
    14. <tr>
    15. <td>
    16. 全选 <input type="checkbox"
    17. v-model="checkAll"
    18. @change="changeAll">
    19. {{checkAll}}
    20. </td>
    21. <td>
    22. 商品
    23. </td>
    24. <td>
    25. 单价
    26. </td>
    27. <td>
    28. 数量
    29. </td>
    30. <td>
    31. 小计
    32. </td>
    33. <td>
    34. 操作
    35. </td>
    36. </tr>
    37. <!--
    38. isSelected: true
    39. productCount: 3
    40. productCover: "xxxxxxx"
    41. productInfo: "颜色:Node.js学习"
    42. productName: "深入浅出Node.js"
    43. productPrice: 57.8
    44. -->
    45. <tr v-for="(item, index) in products" :key="index">
    46. <td>
    47. <input type="checkbox"
    48. @change="changeOne"
    49. v-model="item.isSelected">
    50. </td>
    51. <td>
    52. <img :src="item.productCover" alt="">
    53. {{item.productInfo}}
    54. </td>
    55. <td>
    56. {{item.productPrice}}
    57. </td>
    58. <td>
    59. <input type="number" v-model="item.productCount" min="1">
    60. </td>
    61. <td>
    62. {{item.productPrice * item.productCount | toRMB}}
    63. </td>
    64. <td>
    65. <button class="btn btn-danger"
    66. @click="remove(index)">删除</button>
    67. </td>
    68. </tr>
    69. <tr>
    70. <!--colspan 合并单元格-->
    71. <td colspan="6">总价:{{ sum() | toRMB}}</td>
    72. </tr>
    73. </table>
    74. </div>
    75. </div>
    76. </div>
    77. <script src="axios.js"></script>
    78. <script src="vue.js"></script>
    79. <script>
    80. let vm = new Vue({
    81. el: '#app',
    82. data: {
    83. checkAll: true,
    84. products: []
    85. },
    86. created() {
    87. this.getData();
    88. },
    89. filters: {
    90. toRMB(val, num = 2) {
    91. if (!val) return '0.00';
    92. return '¥' + val.toFixed(num)
    93. }
    94. },
    95. methods: {
    96. getData() {
    97. axios.get('carts.json').then(({data}) => {
    98. this.products = data;
    99. })
    100. },
    101. changeAll() {
    102. // 当 checkAll 选中时,checkAll 是 true,下面的两条都要选中,即这两条的 isSelected 为 true;当 checkAll 没选中的时候,checkAll 是 false,下面的都不选中,即 isSelected 都是 false;所以当 item 的 isSelected 和 checkAll;
    103. this.products.forEach(item => item.isSelected = this.checkAll);
    104. },
    105. changeOne() {
    106. // 点击每个 checkbox 时,去校验每条数据的 isSelected 是否都为 true,如果都为 true,checkAll 就应该为 true;否则,checkAll 就是 false;
    107. // every 方法
    108. this.checkAll = this.products.every(item => item.isSelected);
    109. },
    110. remove(index) {
    111. this.products.splice(index, 1);
    112. },
    113. sum() {
    114. // 计算总价:只计算 checkbox 选中的商品
    115. let selected = this.products.filter(item => item.isSelected);
    116. /*let total = 0;
    117. selected.forEach(item => {
    118. total += item.productPrice * item.productCount;
    119. });
    120. return total;*/
    121. return selected.reduce((prev, next) => {
    122. return prev + next.productPrice * next.productCount
    123. }, 0);
    124. }
    125. }
    126. })
    127. </script>
    128. </body>
    129. </html>