ASCII码 ASCII码

用Vue实现的购物车功能

发布于:2022-04-24 15:51:21  栏目:技术文档

CSS代码:同前篇

html代码:

  1. <div class="box">
  2. <h3>我的购物车(Vue实现)</h3>
  3. <div class="selectAll">
  4. <input type="checkbox" class="check-all" name="check-all" @click="checkAll()" :checked="chka" />
  5. <label for="check-all">全选</label>
  6. </div>
  7. <ul class="list">
  8. <li><span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span></li>
  9. <template v-for="(item,index) of list">
  10. <li>
  11. <input type="checkbox" @click="checkThis(index)" :checked="item.check" />
  12. <span class="content">手机</span>
  13. <input type="number" v-model="item.num" min="1" class="num" />
  14. <span class="price">{{item.price}}</span>
  15. <span class="amount">{{item.price*item.num}}</span>
  16. </li>
  17. </template>
  18. <li>
  19. <span>总计:</span>
  20. <span class="total-num">{{totalNum}}</span>
  21. <span class="total-amount">{{totalAmount}}</span>
  22. </li>
  23. </ul>
  24. <button class="account">结算</button>
  25. </div>

javascript代码:

  1. const app = Vue.createApp({
  2. data() {
  3. return {
  4. chka:true,
  5. list:[{check:true,num:1,price:100},{check:true,num:2,price:200},{check:true,num:3,price:300}],
  6. }
  7. },
  8. computed: {
  9. totalNum(){
  10. return this.list.filter(item=>item.check).reduce((acc,item)=>acc+item.num,0);
  11. },
  12. totalAmount(){
  13. return this.list.filter(item=>item.check).reduce((acc,item)=>acc+item.num*item.price,0);
  14. }
  15. },
  16. methods: {
  17. checkAll(){
  18. this.chka=!this.chka;
  19. this.list.forEach(item=>item.check=this.chka);
  20. },
  21. checkThis(index){
  22. this.list[index].check=!this.list[index].check;
  23. this.chka=this.list.every(item=>item.check==true);
  24. }
  25. },
  26. }).mount('.box')

购物车图示:

相关推荐
阅读 +