当前位置: 首页 > news >正文

el-table 数据去重后合并表尾合计行,金额千分位分割并保留两位小数,表尾合计行表格合并

问题背景

最近在做后台管理项目el-table 时候需要进行表尾合计修改合计后文字的样式合并单元格

想实现的效果

在这里插入图片描述

  1. 合并表尾单元格前三列为1格;
  2. 对某些指定的单元格进行表尾合计;
  3. 合计后的文本样式加粗;
  4. 涉及到金额需要千分位分割并保留两位小数;

涉及到的属性方法

先看下element-plus 中关于 el-table 中能实现上面效果涉及到的属性和方法。

<el-table ref="tableRef":show-summary="true" :summary-method="getSummaries":span-method="spanMethod" 
></el-table>
  • show-summary: 是否在表尾显示合计行;
  • summary-method:自定义的合计计算方法;
  • span-method:合并行或列的计算方法;

el-table表尾合计行 官方文档

表尾合计

对某些指定的单元格进行表尾合计;涉及到金额需要千分位分割并保留两位小数;

<script setup lang="ts">
import { ref, unref, reactive, onMounted } from 'vue'
import type { TableColumnCtx } from 'element-plus'// 模拟接口返回的数据
const data = [{ blocCustNm: 'A', blocLmt: 600, totLnchAmt: 10, totBsnBal: 20, aprvAmt: 30, lnchAmt: 40, bsnBal: 50 },{ blocCustNm: 'B', blocLmt: 200, totLnchAmt: 20, totBsnBal: 30, aprvAmt: 40, lnchAmt: 50, bsnBal: 60 },{ blocCustNm: '', blocLmt: 300, totLnchAmt: 30, totBsnBal: 40, aprvAmt: 50, lnchAmt: 60, bsnBal: 70 }, // blocCustNm为空{ blocCustNm: '', blocLmt: 200, totLnchAmt: 30, totBsnBal: 40, aprvAmt: 50, lnchAmt: 60, bsnBal: 70 }, // blocCustNm为空{ blocCustNm: 'A', blocLmt: 400, totLnchAmt: 40, totBsnBal: 50, aprvAmt: 60, lnchAmt: 70, bsnBal: 80 }, // blocCustNm重复{ blocCustNm: 'C', blocLmt: 500, totLnchAmt: 50, totBsnBal: 60, aprvAmt: 70, lnchAmt: 80, bsnBal: 90 }
]
// 千分位分割,保留小数
function toFixedThousandFilter(num:undefined|number|string, fixed = 2) {if (num === undefined || num === 'undefined' || num === null || num === 'null') return ''return (null || 0).toFixed(fixed).replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}interface Product{blocLmt:string // 集团额度(万元)totLnchAmt:string // 总投放金额(万元)totBsnBal:string // 总业务余额(万元)aprvAmt:string // 审批金额(万元)lnchAmt:string // 投放金额(万元)bsnBal:string // 业务余额(万元)
}
interface SummaryMethodProps<T = Product> {columns: TableColumnCtx<T>[]data: T[]
}
const getSummaries = (param: SummaryMethodProps) => {const { columns, data } = paramconst sums:string[] = []const uniqueBlocCustNm = new Set<string>() // 存储已经处理过的blocCustNm,避免重复计算同个集团客户const sumArr = ['totLnchAmt', 'totBsnBal', 'aprvAmt', 'lnchAmt', 'bsnBal'] // 只对接口返回的这几个字段进行表尾合计columns.forEach((column, index) => {if (index === 0) {sums[index] = '合计'} else if (column.property === 'blocLmt') {let blocLmtSum = 0 // 集团额度总额data.filter((item: anyObj) => {if (!item.blocCustNm || !uniqueBlocCustNm.has(item.blocCustNm)) {blocLmtSum += Number(item[column.property])if (item.blocCustNm) {uniqueBlocCustNm.add(item.blocCustNm)}}})sums[index] = toFixedThousandFilter(blocLmtSum)} else if (sumArr.includes(column.property)) {const values = data.map((item: anyObj) => Number(item[column.property]))sums[index] = getTotalSum(values)}})return sums
}
//封装公共累加方法
const getTotalSum = (values:number[]):string => {if (values.every((value) => Number.isNaN(value))) {return '-'}const total = values.reduce((prev, curr) => {const value = Number(curr)if (!Number.isNaN(value)) {return prev + curr} else {return prev}}, 0)return toFixedThousandFilter(total)
}
</script>

合并表尾单元格前三列为1格

// 合并前三列单元格
const tableRef = ref()
const spanMethod = () => {const current = tableRef.value.$el.querySelector('.el-table__footer-wrapper').querySelector('.el-table__footer')const cell = current.rows[0].cellscell[0].style.textAlign = 'center' // 合计行第一列字段居中显示cell[1].style.display = 'none'cell[2].style.display = 'none' // 隐藏被合并的单元格,不隐藏的话会占位。cell[0].colSpan = '3' // 合并单元格return [1, 1] // 其它单元格按默认显示
}

参考:el-table合计行单元格合并、合并行金额四舍五入保留两位小数、合计行样式修改

http://www.lryc.cn/news/489190.html

相关文章:

  • Springboot整合mybatis-plus使用pageHelper进行分页
  • 【Xbim+C#】创建拉伸的墙
  • 【阅读记录-章节3】Build a Large Language Model (From Scratch)
  • three.js 对 模型使用 视频进行贴图修改材质
  • MySQL - 数据库基础 | 数据库操作 | 表操作
  • maven父子项目
  • NLP论文速读(多伦多大学)|利用人类偏好校准来调整机器翻译的元指标
  • MyBatis——#{} 和 ${} 的区别和动态 SQL
  • 解决sql字符串
  • 深度解析:Android APP集成与拉起微信小程序开发全攻略
  • Leetcode 被围绕的区域
  • ssm框架-spring-spring声明式事务
  • React第五节 组件三大属性之 props 用法详解
  • 测评部署和管理 WordPress 最方便的面板
  • 【系统分析师】-2024年11月论文-论DevOps开发
  • 算法【子数组最大累加和问题与扩展】
  • 小程序23-页面的跳转:navigation 组件详解
  • AI社媒引流工具:解锁智能化营销的新未来
  • 【Node.js】全面解析 Node.js 安全最佳实践:保护您的应用
  • Docker 用法详解
  • Python小游戏28——水果忍者
  • Kafka Offset 自动提交和手动提交 - 漏消费与重复消费
  • Vue3父组件和子组件
  • Linux 定时任务全解析
  • XLNet——打破 BERT 局限的预训练语言模型
  • 开源代码统计工具cloc的简单使用
  • 如何创建一个项目用于研究element-plus的原理
  • 单片机进阶硬件部分_day2_项目实践
  • labview关于文件路径的问题
  • 72项!湖北省2024年度第二批省级科技计划项目拟立项项目公示!