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

Element表格单元格类名动态设置

在 Element UI 的 el-table 组件中,cell-class-name 属性用于动态自定义表格单元格的 CSS 类名,通常用于根据数据条件设置样式。

1. 基本用法

在 el-table 上绑定 :cell-class-name 属性,值为一个函数。该函数接收一个对象参数,返回字符串(类名)或数组(多个类名)。

<el-table :data="tableData" :cell-class-name="cellClassName"
><el-table-column prop="name" label="姓名"></el-table-column><el-table-column prop="age" label="年龄"></el-table-column>
</el-table>

2. 函数参数说明

函数格式:({ row, column, rowIndex, columnIndex }) => className

  • row: 当前行数据

  • column: 当前列配置

  • rowIndex: 行索引(从 0 开始)

  • columnIndex: 列索引(从 0 开始)


3. 示例代码

根据数据条件添加类名
methods: {cellClassName({ row, column, rowIndex, columnIndex }) {// 针对特定列设置样式if (column.property === 'age') {if (row.age > 60) {return 'warning-cell'; // 年龄大于60添加警告样式}}// 针对特定行设置样式if (rowIndex === 0) {return 'first-row-cell'; // 第一行特殊样式}// 默认返回空return '';}
}
CSS 定义样式
.warning-cell {background-color: #fff6f7;color: #ff0000;font-weight: bold;
}.first-row-cell {background-color: #f5f7fa;
}

4. 高级用法

返回多个类名
cellClassName({ row, column }) {const classes = [];if (row.status === 'error') {classes.push('error-cell');}if (column.property === 'name') {classes.push('bold-cell');}return classes; // 返回数组,如 ['error-cell', 'bold-cell']
}
结合列属性判断
cellClassName({ column }) {// 为表头是"操作"的列设置样式if (column.label === '操作') {return 'action-cell';}
}

5. 注意事项

  1. 优先级问题:自定义类名会覆盖 Element 默认样式,必要时使用 !important

  2. 性能优化:避免在函数中执行复杂计算,特别是大数据表格。

  3. 列标识:建议通过 column.property(对应 prop 值)或 column.label 识别列。


完整示例

<template><el-table :data="tableData" :cell-class-name="cellClassName"><el-table-column prop="name" label="姓名"></el-table-column><el-table-column prop="age" label="年龄"></el-table-column><el-table-column prop="status" label="状态"></el-table-column></el-table>
</template><script>
export default {data() {return {tableData: [{ name: '张三', age: 25, status: '正常' },{ name: '李四', age: 70, status: '警告' }]};},methods: {cellClassName({ row, column }) {// 年龄列且值大于60if (column.property === 'age' && row.age > 60) {return 'warning-cell';}// 状态列为"警告"if (column.property === 'status' && row.status === '警告') {return 'error-cell';}}}
};
</script><style>
.warning-cell {background: #fff8e6;color: #ff9900;
}
.error-cell {color: #ff0000;font-weight: bold;
}
</style>

http://www.xdnf.cn/news/16530.html

相关文章:

  • 可控、安全、可集成:安防RTSP|RTMP视频播放模块工程实践参考
  • Android基础(一) 运行HelloWorld
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘ipywidgets’问题
  • 区块链共识机制与联邦学习
  • 《杜甫传》读书笔记与经典摘要(三)流亡与走向人民
  • Java面试题及详细答案120道之(081-100)
  • 电商项目_核心业务_数据归档
  • 计算机网络:(十二)传输层(上)运输层协议概述
  • 【测试报告】玄机抽奖系统(Java+Selenium+Jmeter自动化测试)
  • KNN 算法中的各种距离:从原理到应用
  • PandasAI连接LLM进行智能数据分析
  • Tkinter美化 - 告别土味Python GUI
  • 医疗AI语义潜空间分析研究:进展与应用
  • 2507C++,APC可以干的活
  • 第二阶段-第二章—8天Python从入门到精通【itheima】-138节(MySQL的综合案例)
  • 记录一次薛定谔bug
  • SpringAI入门及浅实践,实战 Spring‎ AI 调用大模型、提示词工程、对话记忆、Adv‎isor 的使用
  • goland编写go语言导入自定义包出现: package xxx is not in GOROOT (/xxx/xxx) 的解决方案
  • red靶机
  • zabbix-agent静默安装
  • AI编程自动化与算法优化实践指南
  • Oracle 19C RU 19.28 升级和安装
  • Spring Cloud 详解与搭建全攻略
  • MySQL的底层原理--InnoDB数据页结构
  • Java实现大根堆与小根堆详解
  • 53. 最大子数组和
  • 在 Windows 系统中实现 WinToGo 的 VHDX 文件切换使用的常见方法
  • 9.3 快速傅里叶变换
  • Cortex-M内核SysTick定时器介绍
  • [2025CVPR-图象合成、生成方向]ODA-GAN:由弱监督学习辅助的正交解耦比对GAN 虚拟免疫组织化学染色