实现修改el-table表格中的字体或背景颜色

需求:计算两数之差,并且在表格中显示,当差大于0,则当前的背景色或字体颜色为绿色,小于0则 显示其他颜色,等于0则正常显示

html 复制代码
<el-table v-loading="loading" :data="list" highlight-current-row stripe border
 :header-cell-style="labelStyle" :cell-style="changeBg" height="580px">
    <el-table-column prop="country" width="100" align="center"></el-table-column>
    <el-table-column width="100" align="center">
        <template slot-scope="{ row }">
            {{ row.num1- row.num2}}
        </template>
    </el-table-column>
    <el-table-column prop="num1" label="数据1" min-width="100" align="center"></el-table-column>
    <el-table-column prop="num2" label="数据2" min-width="100"
                        align="center"></el-table-column>
</el-table>
javascript 复制代码
/* data 部分 */
data() {
    return {
        labelStyle: {   //修改table 头部header样式
            fontSize: '24px',
            fontWeight: 600,
            color: '#585858',
        }
    }
}

/* methods 函数部分 */

// 改变table字体颜色
changeBg({ row, column, rowIndex, columnIndex }) {
            const diff = row.patsanp_patent_num - row.heima_patent_num;
            if (diff > 0 && columnIndex === 1) { // columnIndex 代表第几列
                return {
                    color: '#45A321',
                    fontWeight: 600
                }
            } else if (diff < 0 && columnIndex === 1) {
                return {
                    color: '#d11a32',
                    fontWeight: 600
                }
            } else if (diff == 0 && columnIndex === 1) {
                return {
                    color: '#000',
                }
            }
            else if (columnIndex === 0) {
                // 获取第二列的字体颜色
                const secondColumnColor = this.changeBg({
                    row,
                    column,
                    rowIndex,
                    columnIndex: 1
                }).color;
                return {
                    color: secondColumnColor,
                    fontWeight: 600
                };
            }
}
相关推荐
Quz9 分钟前
QML 与 JavaScript 交互方式:内联函数、外部文件、信号槽与工作线程
javascript·qt
达子66614 分钟前
第10章_HarmonyOs开发图解 用JS开发UI
vue.js
Allshadow33 分钟前
Nest.js - 连接数据库
javascript·nestjs
冰暮流星38 分钟前
javascript之date对象
开发语言·javascript·ecmascript
橘子星38 分钟前
别光看教程!手把手拆解 React Todo 项目,一文吃透 5 个核心概念
前端·javascript
奶糖 肥晨43 分钟前
想return数据 返回的确是函数问题处理
vue.js
蜡台1 小时前
使用 uni-popup 实现数据选择器Data-Picker
前端·javascript·html·uniapp·uni-popup·data-picker
blns_yxl1 小时前
Promise封装Fetch + 重试机制(HTML+JS)
前端·javascript·html
无巧不成书02181 小时前
Vue+Vue-CLI全平台零基础搭建教程
前端·javascript·vue.js·vue·前端开发·vue-cli·前端环境搭建