<el-table:data="billDetailsData"bordershow-summaryid="gy-table":summary-method="getSummaries"style="width: 100%"stripe><el-table-column prop="name" label="项目" min-width="100"/><el-table-column prop="money1" label="金额1"/><el-table-column prop="money2" label="金额2"/><el-table-column prop="money3" label="金额3"/><el-table-column prop="money4" label="金额4"/><el-table-column prop="money5" label="金额5"/><el-table-column prop="money6" label="金额6"/><el-table-column prop="money7" label="金额7"/><el-table-column prop="money8" label="金额8"/></el-table>
引入数字小写转大写方法
import { chineseNumber } from '@/utils/utils'
export function chineseNumber(Num) {// 判断如果传递进来的不是字符的话转换为字符if (typeof Num === 'number') {Num = Num.toString()}Num = Num.replace(/,/g, '') // 替换tomoney()中的“,”Num = Num.replace(/ /g, '') // 替换tomoney()中的空格Num = Num.replace(/¥/g, '') // 替换掉可能出现的¥字符if (isNaN(Num)) { // 验证输入的字符是否为数字// alert('请检查小写金额是否正确');// return '';}// 字符处理完毕后开始转换,采用前后两部分分别转换var part = String(Num).split('.')var newchar = ''// 小数点前进行转化for (var i = part[0].length - 1; i >= 0; i--) {if (part[0].length > 10) {// return '';// 若数量超过拾亿单位,提示}var convert = ''var perchar = part[0].charAt(i)switch (perchar) {case '0':convert = '零' + convertbreakcase '1':convert = '壹' + convertbreakcase '2':convert = '贰' + convertbreakcase '3':convert = '叁' + convertbreakcase '4':convert = '肆' + convertbreakcase '5':convert = '伍' + convertbreakcase '6':convert = '陆' + convertbreakcase '7':convert = '柒' + convertbreakcase '8':convert = '捌' + convertbreakcase '9':convert = '玖' + convertbreak}switch (part[0].length - i - 1) {case 0:convert = convert + '元'breakcase 1:if (perchar !== 0) convert = convert + '拾'breakcase 2:if (perchar !== 0) convert = convert + '佰'breakcase 3:if (perchar !== 0) convert = convert + '仟'breakcase 4:convert = convert + '万'breakcase 5:if (perchar !== 0) convert = convert + '拾'breakcase 6:if (perchar !== 0) convert = convert + '佰'breakcase 7:if (perchar !== 0) convert = convert + '仟'breakcase 8:convert = convert + '亿'breakcase 9:convert = convert + '拾'break}newchar = convert + newchar}// 小数点之后进行转化if (Num.indexOf('.') !== -1) {if (part[1].length > 2) {// alert('小数点之后只能保留两位,系统将自动截断');part[1] = part[1].substr(0, 2)}for (i = 0; i < part[1].length; i++) {convert = ''perchar = part[1].charAt(i)switch (perchar) {case '0':convert = '零' + convertbreakcase '1':convert = '壹' + convertbreakcase '2':convert = '贰' + convertbreakcase '3':convert = '叁' + convertbreakcase '4':convert = '肆' + convertbreakcase '5':convert = '伍' + convertbreakcase '6':convert = '陆' + convertbreakcase '7':convert = '柒' + convertbreakcase '8':convert = '捌' + convertbreakcase '9':convert = '玖' + convertbreak}if (i === 0) convert = convert + '角'if (i === 1) convert = convert + '分'newchar = newchar + convert}}// 替换所有无用汉字while (newchar.search('零零') !== -1) { newchar = newchar.replace('零零', '零') }newchar = newchar.replace('零亿', '亿')newchar = newchar.replace('亿万', '亿')newchar = newchar.replace('零万', '万')newchar = newchar.replace('零元', '元')newchar = newchar.replace('零角', '')newchar = newchar.replace('零分', '')if (newchar.charAt(newchar.length - 1) === '元') {newchar = newchar + '整'}return newchar}
赋值为最后一个值并转大写以及合并
watch: {billDetailsData: {async handler() {await this.$nextTick(()=>{const tds = document.querySelectorAll('#gy-table .el-table__footer-wrapper tr>td');tds[1].innerText = chineseNumber(tds[8].innerText)tds[1].colSpan = 3;tds[1].style.textAlign = 'center'tds[2].style.display = 'none'tds[3].style.display = 'none'});}},},
自动计算各项和
getSummaries(param) {const { columns, data } = param;const sums = [];columns.forEach((column, index) => {if (index === 0) {sums[index] = '总价';return;}if (index === 4) {sums[index] = '小写';return;}const values = data.map(item => Number(item[column.property]));if (!values.every(value => isNaN(value))) {sums[index] = values.reduce((prev, curr) => {const value = Number(curr);if (!isNaN(value)) {return prev + curr;} else {return prev;}}, 0);// sums[index] += ' 元';} else {sums[index] = 'N/A';}});return sums;},