1.本地数据

image.png

  1. // data/carts.json
  2. [
  3. {
  4. "isSelected": true,
  5. "productCover": "https://img3.doubanio.com/view/subject/m/public/s33510542.jpg",
  6. "productName": "纸上行舟",
  7. "productInfo": "青年作者黎幺短篇小说首度结集",
  8. "productPrice": 57.33,
  9. "productCount": 3,
  10. "id": "0001"
  11. },
  12. {
  13. "isSelected": true,
  14. "productCover": "https://img3.doubanio.com/view/subject/m/public/s33497735.jpg",
  15. "productName": "我可能得抑郁症了!舟",
  16. "productInfo": "青年作者黎幺短篇小说首度结集",
  17. "productPrice": 54.26,
  18. "productCount": 2,
  19. "id": "0002"
  20. },
  21. {
  22. "isSelected": true,
  23. "productCover": "https://img3.doubanio.com/view/subject/m/public/s33474961.jpg",
  24. "productName": "绕日飞行",
  25. "productInfo": "驯马、飞行、成长、爱情",
  26. "productPrice": 22.67,
  27. "productCount": 3,
  28. "id": "0003"
  29. }
  30. ]

2.使用axios获取本地数据

  1. var vm=new Vue({
  2. el:"#app",
  3. data:{
  4. products:[],
  5. },
  6. mounted() {
  7. axios.get('./data/carts.json').then(res=>{
  8. return this.products=res.data;
  9. })
  10. this.handleItem() //进入这个函数时就执行一下handleItem()
  11. },
  12. })

3.使用数据

  1. <table class="table table-bordered table-hover">
  2. <thead>
  3. <tr>
  4. <th>全选 <input type="checkbox" v-model="checkAll" @change="change"></th>
  5. <th>商品</th>
  6. <th>单价</th>
  7. <th>数量</th>
  8. <th>小计</th>
  9. <th>操作</th>
  10. </tr>
  11. </thead>
  12. <tbody>
  13. <tr v-for="item of products">
  14. <td> <input type="checkbox" v-model="item.isSelected" @change="handleItem"></td>
  15. <td class="name"><img class="img" :src="item.productCover" alt=""></img>{{item.productName}}</td>
  16. <td>{{item.productPrice}}</td>
  17. <td><input type="number" class="number" min="1" v-model.number="item.productCount"></td>
  18. <td>{{item.productPrice*item.productCount | format(2)}}</td>
  19. <td><button class="delete btn-danger">删除</button></td>
  20. </tr>
  21. </tbody>
  22. </table>