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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <meta name=referrer content=never>
    9. <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
    10. <script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
    11. <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet">
    12. <style>
    13. .img{
    14. width: 30px;
    15. height: 50px;
    16. }
    17. .name{
    18. font-size: 13px;
    19. }
    20. .number{
    21. width: 40px;
    22. }
    23. .delete{
    24. width: 70px;
    25. height: 40px;
    26. border: none;
    27. border-radius: 5px;
    28. }
    29. </style>
    30. </head>
    31. <body>
    32. <div id="app" class="container">
    33. <h2 class="text-center text-success">购物车</h2>
    34. <table class="table table-bordered table-hover">
    35. <thead>
    36. <tr>
    37. <th>全选 <input type="checkbox" v-model="checkAll" ></th>
    38. <th>商品</th>
    39. <th>单价</th>
    40. <th>数量</th>
    41. <th>小计</th>
    42. <th>操作</th>
    43. </tr>
    44. </thead>
    45. <tbody>
    46. <tr v-for="(item,index) of products">
    47. <td> <input type="checkbox" v-model="item.isSelected"></td>
    48. <td class="name"><img class="img" :src="item.productCover" alt=""></img>{{item.productName}}</td>
    49. <td>{{item.productPrice}}</td>
    50. <td><input type="number" class="number" min="1" v-model.number="item.productCount"></td>
    51. <td>{{item.productPrice*item.productCount | format(2)}}</td>
    52. //<td><button class="delete btn-danger" @click="handleDelete(index)">删除</button></td>
    53. <td><button class="delete btn-danger" @click="handleDelete(item)">删除</button></td>
    54. </tr>
    55. </tbody>
    56. </table>
    57. <p class="text-left">总价格:{{sum | format(2)}}</p>
    58. </div>
    59. <script>
    60. /* ctrl+shift+p */
    61. var vm=new Vue({
    62. el:"#app",
    63. data:{
    64. products:[],
    65. },
    66. //页面装载时执行
    67. mounted() {
    68. axios.get('./data/carts.json').then(res=>{
    69. return this.products=res.data;
    70. })
    71. },
    72. filters:{
    73. format(val,params){
    74. return "$"+val.toFixed(params);
    75. }
    76. },
    77. computed: {
    78. checkAll:{
    79. get(){
    80. return this.products.every(item=>item.isSelected)
    81. },
    82. set(val){
    83. this.products.forEach(item=>item.isSelected=val)
    84. }
    85. },
    86. sum(){
    87. var total=0;
    88. this.products.forEach(item=>{
    89. total=item.productCount*item.productPrice+total
    90. })
    91. return total
    92. }
    93. },
    94. methods: {
    95. /* handleDelete(index){
    96. this.products.splice(index,1)
    97. } */
    98. handleDelete(item){
    99. var products=this.products.filter(value=>value!==item);
    100. this.products=products;
    101. }
    102. },
    103. })
    104. </script>
    105. </body>
    106. </html>