今天需求中有一条要求使用table表格要有两条合计项,要求实现效果:
主要代码:
summaryMethod(param) {
const { columns, data } = param
const sums = []
columns.forEach((column, index) => {
if (index === 0) {
const el = <p>合计:<br/><br/>总计:</p> // 主要代码
sums[index] = el
return
}
if (column.property == 'budgerAllPrice') { // 如果el-table-column中没有prop属性,这里的column.property是undefined,所以判断一下values数组是否为空
const values = data.map((item) => {
return Number(item.budgerAllPrice)
})// 把对应一列中的之全部取出,放到一个数组中
if (!values.every(value => isNaN(value))) {
let itemPrice = 0
sums[index] = values.reduce((prev, curr) => {
const value = Number(curr)// 将values中的每个值转换为number类型
if (!isNaN(value)) {
itemPrice += Number(curr)
// this.tableData.goodstotal + this.tableData.accessorytotal + this.tableData.optiontotal
const pel = <p>¥{this.$options.filters['money'](itemPrice)}元<br/><br/>¥{this.$options.filters['money'](vm.totalPrice)}元</p> // 主要代码
return pel
} else {
return this.$options.filters['money'](prev)
}
}, 0)
} else {
sums[index] = '无'
}
}
})
return sums
},
其中table表格需要绑定:summary-method="summaryMethod",这里可以参考ElementUI官方文档https://element.eleme.cn/#/zh-CN/component/table
主要是记录一下elementUI中部常用的属性,以防自己忘记