element中el-table表头通过header-row-style设置样式

文章目录

一、知识点

有些时候需要给element-ui表头设置不同样式,比如居中、背景色、字体大小等等,这时就可以用到本文要说的属性header-row-style。官网说明如下所示:

二、设置全部表头

2.1、方式一

html 复制代码
<el-table :header-cell-style="{'text-align': 'center'}" />

2.2、方式二

vue 复制代码
<template>
	<el-table :header-cell-style="tableHeaderColor" />
</template>
<script>
export default {
	methods: {
		tableHeaderColor ({row, column, rowIndex, columnIndex}) {
			return 'text-align: center;'
		}
	}
}
</script>

三、设置某个表头

vue 复制代码
<template>
	<el-table :header-cell-style="tableHeaderColor" />
</template>
<script>
export default {
	methods: {
		// 设置表头的颜色
		tableHeaderColor({ row, column, rowIndex, columnIndex }) {
			console.log(row, column, rowIndex, columnIndex);
			if (rowIndex === 0 && columnIndex === 1) {
				return 'background-color: #afccfd; color:#000000;'; //蓝色
			} else if (rowIndex === 0 && columnIndex === 2) {
				return 'background-color: #c0e33c; color:#000000;';//绿色
			} else if (rowIndex === 0 && columnIndex === 3) {
				return 'background-color: #fbc57b; color:#000000;';//橙色
			} else {
				return 'color:#000000; background: #ffffff;';
			}
		}
	}
}
</script>

效果如下所示:

四、最后

本人每篇文章都是一字一句码出来,希望大佬们多提提意见。顺手来个三连击,点赞👍收藏💖关注✨。创作不易,给我打打气,加加油☕

相关推荐
Moonbit6 小时前
招募进行时 | MoonBit AI : 程序语言 & 大模型
前端·后端·面试
excel6 小时前
Vue 3 编译器源码深度解析:transformOn —— v-on 指令的编译过程
前端
excel6 小时前
Vue 编译器核心:transformIf 模块深度解析
前端
CodeToGym6 小时前
Vue2 和 Vue3 生命周期的理解与对比
前端·javascript·vue.js
excel6 小时前
深度解析 Vue 编译器源码:transformFor 的实现原理
前端
excel6 小时前
Vue 编译器源码精读:transformBind —— v-bind 指令的编译核心
前端
excel6 小时前
深入浅出:Vue 编译器中的 transformText —— 如何把模板文本变成高效的渲染代码
前端
excel6 小时前
Vue 编译器源码深析:transformSlotOutlet 的设计与原理
前端
excel6 小时前
Vue 编译器核心源码解读:transformElement.ts
前端
excel6 小时前
Vue 编译器兼容性系统源码详解
前端