Vue 学习随笔系列十七 -- 表格样式修改

表格样式修改

文章目录


一、表格背景颜色修改

1、方法一

表格外套一个 div ,修改div的背景色,并将表格背景色设置为透明

参考代码:

bash 复制代码
<template>
    <div class="custom-table">
        <el-table
            size="mini"
            border
            :data="tableData"
            :max-height="tableHeight"
            style="width: 100%">
            <el-table-column
                v-for="(item, index) in tableColumns"
                :key="index"
                v-bind="item"
                align="center"
                >
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="100"
            >
                <template slot-scope="scope">
                    <span
                        class="del-btn"
                        @click="emitEvent({eventName:'deleteItem',params:scope.row})"
                    >删除</span>
                </template>
            </el-table-column>           
        </el-table>
    </div>
</template>

<script>
    export default {
        props: { 
            tableData: {
                type: Array,
                default: () => []
            },
            tableHeight: {},
            tableColumns: {
                default: () => []
            }
        },
        data() {
            return { }
        }, 
    }
</script>

<style lang="less" scoped>
    .del-btn {
        color: #4597EB;
        cursor: pointer;
    }

    .custom-table /deep/ .el-table, .el-table__expaned-cell {
        background-color: transparent;
    }
    .custom-table /deep/ .el-table tr {
        background-color: transparent !important;
    }
    .custom-table /deep/  .el-table--enable-row-transition .el-table__body td, .el-table .cell{
        background-color: transparent;
    }
    .custom-table /deep/ .el-table th.el-table__cell {
        background: transparent !important; // 表头背景透明
    }
    .el-table::before { //去除底部白线
        left: 0;
        bottom: 0;
        width: 100%;
        height: 0px;
    }
</style>

2、方法二

直接修改表格背景颜色

bash 复制代码
<template>
    <div class="custom-table">
        <el-table
            size="mini"
            border
            :data="tableData"
            :max-height="tableHeight"
            style="width: 100%">
            <el-table-column
                v-for="(item, index) in tableColumns"
                :key="index"
                v-bind="item"
                align="center"
                >
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="100"
            >
                <template slot-scope="scope">
                    <span
                        class="del-btn"
                        @click="emitEvent({eventName:'deleteItem',params:scope.row})"
                    >删除</span>
                </template>
            </el-table-column>           
        </el-table>
    </div>
</template>

<script>
    export default {
        props: { 
            tableData: {
                type: Array,
                default: () => []
            },
            tableHeight: {},
            tableColumns: {
                default: () => []
            }
        },
        data() {
            return {
                
            }
        },
        methods: {
            emitEvent ({ eventName, params }) {
                this.$emit(eventName, params)
            }
        }, 
    }
</script>

<style lang="less" scoped>
    .del-btn {
        color: #4597EB;
        cursor: pointer;
    }
    .custom-table {
        background: transparent;
    }
    .custom-table /deep/ .el-table th.el-table__cell,
    /deep/ .el-table tr,
    /deep/.el-table th {
    	color: '#FFF'; // 表格字体颜色
        background: red; // 表格背景颜色
    }

</style>

二、多级表头颜色修改

bash 复制代码
<template>
   <el-table 
	   :data="tableData" 
	   :header-cell-style="tableHeaderColor" 
	   border 
	   stripe
   >
</template>

<script>
export default {
    methods: {
         // 修改表头颜色
        tableHeaderColor ({ rowIndex, columnIndex }) {
            if (rowIndex === 0 && columnIndex === 0) {
                return "background:#D9EAFFFF;";
            }
            if (rowIndex === 0 && columnIndex === 2) {
                return "background: #FFF0E1FF;";
            }
            if (rowIndex === 0 && columnIndex === 3) {
                return "background: #E0F5E9FF;";
            }
            if (rowIndex === 0 && columnIndex === 4) {
                return "background: #D9EAFFFF;";
            }
            if (rowIndex === 0 && columnIndex === 5) {
                return "background: #FFF0E1FF;";
            }
            if (rowIndex === 0 && columnIndex === 6) {
                return "background: #E0F5E9FF;";
            }
        },
    }
}
</script>

<style>

</style>
相关推荐
萌萌哒草头将军1 分钟前
🚀🚀🚀恭喜 Rolldown 获得 JS 开源奖年度突破奖,2025 年 JS 开源奖典礼回顾!🎉🎉🎉
前端·vue.js·react.js
Mr_Wu20182 分钟前
Electron截取响应体
前端·javascript·electron
江城开朗的豌豆9 分钟前
nextTick vs setTimeout:Vue中的'马上'和'等会儿'到底差在哪?
前端·javascript·vue.js
江城开朗的豌豆16 分钟前
Vue的nextTick:你以为DOM更新完就真的更新完了?
前端·javascript·vue.js
jarenyVO17 分钟前
RabbitMQ全面学习指南
数据库·学习·rabbitmq
琪阿不会编程17 分钟前
7.Vue的compute计算属性
前端·javascript·vue.js
大磕学家ZYX18 分钟前
JavaScript学习笔记
javascript·笔记·学习
网络点点滴18 分钟前
使用Netlify进行简单部署
javascript
BillKu22 分钟前
Vue3 + Element Plus 获取表格列信息
javascript·vue.js·elementui
海的诗篇_25 分钟前
前端开发面试题总结-vue2框架篇(二)
前端·javascript·css·vue.js·前端框架·vue