elementUi中的el-table表格的内容根据后端返回的数据用不同的颜色展示

效果图如下:

首先

首先:需要在表格行加入 <template slot-scope="{ row }"> </template>标签

复制代码
 <el-table-column  prop="usable" align="center" label="状态" width="180" >
            <template slot-scope="{ row }">
              <span :class="fontLightClass(row.usable)">{{  row.usable ==true ? "启用" : "停用"}}</span>
            </template>
          </el-table-column>

2.在methods里面加入这个方法:

复制代码
  fontLightClass(usable){
      if(usable == true){
    		return 'fgreen'
    }else{
      return 'fred'
    }
    },

3.去设置自己喜欢的颜色:

复制代码
<style scoped>

.fred {
  color: red;
}
.fgreen {
  color: green;
}

</style>

//还有另一种(这种后端返回的数据里面带有背景色的字段)

复制代码
 <el-table-column prop="risk_level_name" align="center" label="风险等级"   width="180" >
          <template slot-scope="{ row }">
            <div
              :style="{
                marginLeft: 18 + '%',
                textAlign: 'center',
                width: 98 + 'px',
                color: '#333333 !important',
                backgroundColor: 'rgb(' + row.color_value + ')',
              }">
              <label>{{ row.risk_level_name }}</label>
            </div>
          </template>
        </el-table-column>
相关推荐
用户23678298016810 分钟前
Canvas:实现一个高颜值二维码生成器
javascript
剑神一笑25 分钟前
从字符串到时间线:实现一个 Cron 表达式解析器
javascript·typescript·reactjs
前端那点事40 分钟前
深度解析:Vue中computed的实现原理(易懂不晦涩)
前端·vue.js
前端那点事42 分钟前
Vue中深克隆的3种实现方案(附详细注释+测试)
前端·vue.js
❆VE❆1 小时前
基于 contenteditable 实现变量插入富文本编辑器
前端·javascript·vue.js
烤麻辣烫1 小时前
json与fastjson
前端·javascript·学习·json
小陈同学呦1 小时前
JavaScript 深浅拷贝详解
前端·javascript
小陈同学呦1 小时前
fetch和axios区别
前端·javascript
森叶2 小时前
Electron 实战:用 utilityProcess 开子进程,去端口化承载协议处理,并由主进程拦截渲染请求后统一中转
前端·javascript·electron
Hilaku3 小时前
做了 6 年前端,技术不差却拿不到 Offer?
前端·javascript·程序员