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>
相关推荐
@atweiwei3 小时前
LangChainRust 0.33.0 发布:让 Agent 应用更容易运行、观察和评测
服务器·前端·javascript
AI开源软件7 小时前
用MQTT跑通一台模拟设备:看懂IoT产品、物模型和告警
java·vue.js·spring boot·物联网·mqtt·iot·企业管理
qq_274475159 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/any
开发语言·javascript
GuWenyue11 小时前
昵称限 20 字,8 个 emoji 却超长:一个「字」在系统里有四种长度
javascript·数据库·后端
画中有画14 小时前
自动化脚本中使用RSA
前端·javascript·自动化
只睡四小时15 小时前
AI 写的倒计时:Electron 每轮吞 0.8 秒
javascript·人工智能·性能优化·electron·时间管理·番茄钟
全速向光16 小时前
用 Three.js 从零打造一棵会发光的 3D 圣诞树
开发语言·javascript·3d
布语world16 小时前
某东pc滑块分析
前端·javascript·网络·python·安全
map_vis_3d17 小时前
百度地图自定义覆盖物 CustomOverlay:用 HTML、React、Vue 组件做标注
前端·javascript·vue·react·百度地图
变与不变80617 小时前
js数据流
前端·javascript