php 全选计算总价格,vue2.0中vue-cli实现全选、单选计算总价格的实例代码

由于工作的需要并鉴于网上的vue2.0中vue-cli实现全选、单选方案不合适,自己写了一个简单实用的。就短短的126行代码。

全选({{checkedCount}})产品名称价格数量
我被选中{{item.productName}}{{item.price}}{{item.count}}

总价:{{totalMoney}}

export default{

data() {

return {

checked:[],

totalPrice:[],

lists : [

{

productName:'产品1',

price:'24',

count:'3',

id:1

},

{

productName:'产品2',

price:'25',

count:'6',

id:2

},

{

productName:'产品3',

price:'54',

count:'7',

id:3

}

]

}

},

computed:{

totalMoney:function(item,index){

let sum = 0;

for(let i=0;i

sum += this.totalPrice[i];

};

return sum;

},

checkAll: {

get: function() {

return this.checkedCount == this.lists.length;

},

set: function(value){

var _this = this;

if (value) {

this.totalPrice = [];

this.checked = this.lists.map(function(item) {

item.checked = true;

let total = item.price*item.count;

_this.totalPrice.push(total);

return item.id

})

}else{

this.checked = [];

this.totalPrice=[];

this.lists.forEach(function(item,index){

item.checked = false;

});

}

}

},

checkedCount: {

get: function() {

return this.checked.length;

}

}

},

methods:{

currClick:function(item,index){

var _this = this;

if(typeof item.checked == 'undefined'){

this.$set(item,'checked',true);

let total = item.price*item.count;

this.totalPrice.push(total);

console.log(this.totalPrice);

}else{

item.checked = !item.checked;

if(item.checked){

this.totalPrice = [];

this.lists.forEach(function(item,index){

if(item.checked){

let total = item.price*item.count;

_this.totalPrice.push(total);

}

});

}else{

this.totalPrice = [];

this.lists.forEach(function(item,index){

if(item.checked){

let total = item.price*item.count;

_this.totalPrice.push(total);

}

});

}

}

}

}

}

tr td{

width:200px;

background: #eee;

padding:10px 0;

}

效果:

75fd63a146749f4ec68dfb21e6617f0a.png

64f70abfa2ebb8e935fd5989bbb0db59.png

68efda220af4c3e3a79e3d9e3fb5ec21.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值