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

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.dtcms.com/a/300470.html

相关文章:

  • Linux网络
  • libomxil-bellagio移植到OpenHarmony
  • Ubuntu简述及部署系统
  • MybatisPlus-19.插件功能-通用分页实体
  • JDK 11.0.16.1 Windows 安装教程 - 详细步骤+环境变量配置
  • Day44 Java数组08 冒泡排序
  • AI与区块链Web3技术融合:重塑数字经济的未来格局
  • SpringSecurity实战:核心配置技巧
  • 【前端】【vscode】【.vscode/settings.json】为单个项目配置自动格式化和开发环境
  • 【C++基础】类型转换:static_cast/dynamic_cast 面试高频考点与真题解析
  • Spring Retry 异常重试机制:从入门到生产实践
  • ESP32学习-FreeRTOS队列使用指南与实战
  • 【多模态】天池AFAC赛道四-智能体赋能的金融多模态报告自动化生成part2-报告输出
  • Java面试实战:企业级性能优化与JVM调优全解析
  • 小白成长之路-Ansible自动化(一)
  • 将远程 main 分支同步到 develop 分支的完整指南
  • 【硬件】嵌入式软件开发(2)
  • STM32-USART串口实现接收数据三种方法(1.根据\r\n标志符、2.空闲帧中断、3.根据定时器辅助接收)
  • Pytest 参数化进阶:掌握 parametrize 的多种用法
  • HCIP---MGRE实验
  • 嵌入式硬件篇---ESP32稳压板
  • OpenLayers 综合案例-轨迹回放
  • LeetCode|Day27|70. 爬楼梯|Python刷题笔记
  • catkin_make与catkin build的关系与区别(使用catkin build的好处)
  • MGRE实验
  • 深入解析 Vue 3 中 v-model 与表单元素的绑定机制
  • 多租户Kubernetes集群架构设计实践——隔离、安全与弹性扩缩容
  • Spring Boot自动配置原理深度解析
  • 昇思学习营-模型推理和性能优化
  • Keepalived + LVS-DR 高可用与负载均衡实验