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

vue2+elementui 表格单元格增加背景色,根据每列数据的大小 颜色依次变浅显示

注释:
vue2+elementui 表格列实现一个功能,给定两个颜色:红色 #f96d6f 和 绿色 #63be7b,列数据正数时表格单元格背景色为红色,列数据负数时表格单元格背景色为绿色,根据数据的大小颜色依次越来越淡,最大的正数颜色最红,剩下的正数从大到小依次根据这个红色变浅,最小的负数颜色最绿,剩下的负数从小到大依次根据这个绿色变浅。
此方法中最后一行数据 颜色固定显示,有做单独处理,不参与这个方法

封装一个js方法:

/*** 增强版表格颜色渐变方法* @param {Array} columnData 当前列所有数据* @param {String} baseColor 基础色(#f96d6f/#63be7b)* @param {Number} currentValue 当前单元格值* @returns {String} 背景色样式*/
function getEnhancedColor(columnData, baseColor, currentValue) {// 分离正负数并去重排序const positives = [...new Set(columnData.filter(v => v > 0))].sort((a,b) => b-a);const negatives = [...new Set(columnData.filter(v => v < 0))].sort((a,b) => a-b);// 计算动态透明度(0.2-1.0区间)let opacity = 0.2;if (currentValue > 0 && positives.length) {const rank = positives.indexOf(currentValue);opacity = 0.2 + (0.8 * (1 - rank/positives.length));} else if (currentValue < 0 && negatives.length) {const rank = negatives.indexOf(currentValue);opacity = 0.2 + (0.8 * (1 - rank/negatives.length));}else {return ''; // 零值不染色}// HEX转RGBAconst r = parseInt(baseColor.slice(1,3), 16);const g = parseInt(baseColor.slice(3,5), 16);const b = parseInt(baseColor.slice(5,7), 16);return `background-color: rgba(${r}, ${g}, ${b}, ${opacity.toFixed(2)});`;
}/*** ElementUI表格列颜色处理器* @param {Object} params 单元格参数* @param {Array} allData 表格数据* @param {String} prop 字段名*/
export function columnColorHandler(params, allData, prop) {const { row,rowIndex } = params;const columnData = allData.map(item => item[prop]);const value = row[prop];// 最后一行特殊处理if (rowIndex === allData.length - 1) {return "background-color: #990000; color: #fff; font-weight: bold;";}if (value > 0) {return getEnhancedColor(columnData, '#f96d6f', value);} else if (value < 0) {return getEnhancedColor(columnData, '#63be7b', value);}return '';}

表格中使用(:cell-style=“cellStyle”)

<el-tableborder stripe v-loading="isLoading"style="width: 85%;margin: 20px auto;"highlight-current-row:header-cell-style="headerCellStyle()":cell-style="cellStyle"@sort-change="sortChange":data="tableData"ref=""><el-table-column prop="industryName" label="行业" align="center" min-width="100" show-overflow-tooltip><template slot-scope="scope"><div>{{ scope.row.industryName || '-' }}</div></template></el-table-column><el-table-column prop="indWeight" label="组合权重" align="center" sortable="custom" min-width="95"><template slot-scope="scope"><div>{{ formatterData(scope.row.indWeight) }}</div></template></el-table-column><el-table-column prop="bmIndWeight" label="基准权重" align="center" sortable="custom" min-width="95"><template slot-scope="scope"><div>{{ formatterData(scope.row.bmIndWeight) }}</div></template></el-table-column><el-table-column prop="exWeight" label="超额" align="center" sortable="custom"><template slot-scope="scope"><div>{{ formatterData(scope.row.exWeight) }}</div></template></el-table-column></el-table>

cellStyle方法中设置单元格背景色:column.property 根据实际情况来,哪一列需要就添加哪一列:

cellStyle({row, column, rowIndex, columnIndex}) {if(column.property === 'indWeight' || column.property === 'bmIndWeight' || column.property === 'exWeight' || column.property === 'indReturn' || column.property === 'bmIndReturn'|| column.property === 'exReturn' || column.property === 'iait' || column.property === 'ssit' || column.property === 'init1' || column.property === 'total'){return columnColorHandler({ row, column, rowIndex, columnIndex },this.tableData,column.property);}},

这个方法最终效果是根据给定的基础色:
红色#f96d6f: 数据从大到小颜色依次变浅;
绿色 #63be7b: 数据从小到大颜色依次变浅。

http://www.dtcms.com/a/351669.html

相关文章:

  • 科研笔记:SCI论文中的功能性图表
  • 【技术教程】如何将文档编辑器集成到用 .Net 编写的网络应用程序中
  • VScode,设置自动保存
  • 支持向量机学习
  • Ubuntu22.04 安装和使用标注工具labelImg
  • GZ-CTF平台pwn题目部署
  • GitHub 热榜项目 - 日榜(2025-08-26)
  • word批量修改交叉引用颜色
  • 【RAGFlow代码详解-28】部署和基础设施
  • 国标28181 国标视频平台
  • 四、Python 脚本常用模块(续)
  • Linux虚拟机ansible部署
  • 机器视觉学习-day04-形态学变换
  • Spring Boot 与传统 Spring:从 WAR 到可执行 JAR,颠覆性的部署哲学
  • MEMS陀螺定向短节与传统陀螺工具的区别?
  • 永磁同步电机无速度算法--传统脉振方波注入法(1)
  • 图片生成视频软件深度评测:浅谈视频音频提取技术
  • Boris FX Samplitude Suite 2025.0.0 音频录制/编辑和母带处理
  • 不增加 GPU,首 Token 延迟下降 50%|LLM 服务负载均衡的新实践
  • 如何基于阿里云OpenSearch LLM搭建智能客服平台
  • ssc37x平台的音频应用demo
  • 160.在 Vue3 中用 OpenLayers 解决国内 OpenStreetMap 地图加载不出来的问题
  • Mamba-HoME:面向3D医学影像分割的层次化专家混合新框架
  • 蓝思科技半年净利超11亿,蓝思成绩单怎么分析?
  • 为什么选择爱普生TG5032CFN温补晶振—可穿戴设备?
  • Pycharm
  • 前端漏洞(上)- JSONHijacking 漏洞
  • LangGraph-2-Demo
  • 基于goofys挂载s3到ubuntu, 没用s3fs因为它兼容性不太好
  • Stream流中的Map与flatMap的区别