实现步骤:
自定义数组对象合计方法:
countTotal(arr, keyName) {
let $total = 0
$total = arr.reduce(function(total, currentValue, currentIndex, arr) {
return currentValue[keyName] ? (total + currentValue[keyName]) : total
}, 0)
return $total
}
勾选复选框时,调用合计方法即可:
selectionChangeHandler(val) {
if (val.length !== 0) {
this.totalNumer = this.countTotal(val, 'shipped')
}
}
效果: