• 参考资料:vue.js官网
  • 项目演示:

vue购物车和地址选配(三)

  • 项目源代码:
  • 核心代码及踩坑

删除:

new Vue({
    el:'#app',
    data:{
      productlist:[],
      totalMoney:0,
      checkAllFrag:false,//默认没有全选
      deFlag:false,
      //当前的存起来
      curProduct:''
    },
    //必须加mounted函数,这是页面初加载,如果不写这个函数,network中将请求不到数据
    mounted:function(){
          this.cartView();
    },

    //局部过滤器
    filters:{

        formatMoney:function(value){
            return "$" + value.toFixed(2);
        }
    },
    
    methods:{

        cartView:function(){
            
        var _this=this;    //要保存这个this,
        this.$http.get('data/cartData.json',{'id':'123'}).then(function(res){
                  
          _this.productlist=res.data.result.list;   //这里的this已经不是实例对象了
        });

        },

        changeMoney:function(product,way){
         
         if(way>=1){
          product.productQuantity++;
         }else{
               product.productQuantity--;
             if(product.productQuantity<1){
                 product.productQuantity=1;
             }
         }
          this.calcTotalPrice();

        },

        selectedProduct:function(item){

            //每次选中的时候先判断当前这个item.checked属性是否存在
            if(typeof item.check=="undefined"){
                this.$set(item,'check',true);//如果不存在就先给他设置一个
            }else{
                item.check=!item.check;
            }

            this.calcTotalPrice();
        },

        //
        checkAll:function(flag){

           
            this.checkAllFrag=flag; //将前面传来的标志记录下来,全选=true,不全选=false
             var _this=this;
         
              _this.productlist.forEach(function(item,index){ //就把上面的单选按钮的check都设置为true

                if(typeof item.check =="undefined"){
                  _this.$set(item,'check',_this.checkAllFrag); //把单选按钮设置成和全选时=true,不全选时=false
                }else{
                  item.check=_this.checkAllFrag;
                }
              });

              this.calcTotalPrice();
        },

        calcTotalPrice:function(){

          var _this=this;
          _this.totalMoney=0;//每次计算都要清零处理

          _this.productlist.forEach(function(item,index){

            if(item.check){//如果被选中,就计算总金额,并且把每一项累加
               _this.totalMoney+=item.productPrice*item.productQuantity;
            }
         

          });
        },

        delProduct:function(){
           //
         //  this.productlist.indexOf(this.curProduct);

           this.productlist.splice(this.index,1);
           this.deFlag=false;
         
        }

    }

    
   
});

Vue.filter('money',function(value,type){
    return '$' + value.toFixed(2)+type;
});
删除的实例.js

相关文章:

  • 2021-06-18
  • 2022-12-23
  • 2021-11-10
  • 2021-07-21
  • 2021-07-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
  • 2021-08-01
  • 2022-12-23
  • 2021-08-31
  • 2022-02-10
相关资源
相似解决方案