Element UI表格中根据数值动态设置字体颜色

我们有一个温度监控系统,需要展示温度偏差值:

  • 正偏差值(高于标准温度)显示为红色

  • 负偏差值(低于标准温度)显示为蓝色

  • 零或空值显示为默认颜色

一、动态Class绑定

html 复制代码
<el-table-column sortable="custom" prop="deviationValue" label="偏差值(℃)" width="75">
  <template #default="scope">
    <div 
      class="openParamsClass over-emplis" 
      :class="getDeviationClass(scope.row.deviationValue)"
      @click="chartOpen(scope.row.id, 'deviationValue', '偏差值')" 
      :title="scope.row.deviationValue"
    >
      {{ scope.row.deviationValue != null ? scope.row.deviationValue : '-' }}
    </div>
  </template>
</el-table-column>

css

css 复制代码
.deviation-positive {
  color: #ff4d4f;
  font-weight: 500;
}

.deviation-negative {
  color: #1890ff;
  font-weight: 500;
}

.deviation-zero {
  color: #606266;
}

二、动态Style绑定

html 复制代码
<el-table-column sortable="custom" prop="deviationValue" label="偏差值(℃)" width="75">
  <template #default="scope">
    <div 
      class="openParamsClass over-emplis" 
      :style="getDeviationStyle(scope.row.deviationValue)"
      @click="chartOpen(scope.row.id, 'deviationValue', '偏差值')" 
      :title="scope.row.deviationValue"
    >
      {{ scope.row.deviationValue != null ? scope.row.deviationValue : '-' }}
    </div>
  </template>
</el-table-column>

方法:

javascript 复制代码
methods: {
  getDeviationStyle(value) {
    if (value === null || value === undefined || value === '-') {
      return { color: '#606266' };
    }
    
    const numValue = Number(value);
    if (numValue > 0) {
      return { color: '#ff4d4f', fontWeight: '500' };
    } else if (numValue < 0) {
      return { color: '#1890ff', fontWeight: '500' };
    } else {
      return { color: '#606266' };
    }
  }
}

三、模板内联判断

html 复制代码
<el-table-column sortable="custom" prop="deviationValue" label="偏差值(℃)" width="75">
  <template #default="scope">
    <div 
      class="openParamsClass over-emplis" 
      :style="{
        color: scope.row.deviationValue > 0 ? '#ff4d4f' : 
               scope.row.deviationValue < 0 ? '#1890ff' : '#606266',
        fontWeight: scope.row.deviationValue !== 0 ? '500' : 'normal'
      }"
      @click="chartOpen(scope.row.id, 'deviationValue', '偏差值')" 
      :title="scope.row.deviationValue"
    >
      {{ scope.row.deviationValue != null ? scope.row.deviationValue : '-' }}
    </div>
  </template>
</el-table-column>
相关推荐
晚霞的不甘18 分钟前
Flutter for OpenHarmony从基础到专业:深度解析新版番茄钟的倒计时优化
android·flutter·ui·正则表达式·前端框架·鸿蒙
东东51622 分钟前
果园预售系统的设计与实现spingboot+vue
前端·javascript·vue.js·spring boot·个人开发
怪兽毕设1 小时前
基于SpringBoot的选课调查系统
java·vue.js·spring boot·后端·node.js·选课调查系统
Amumu121381 小时前
Vue Router(一)
前端·javascript·vue.js
切糕师学AI1 小时前
VSCode 下如何检查 Vue 项目中未使用的依赖?
vue.js·vscode
我是伪码农2 小时前
Vue 1.30
前端·javascript·vue.js
利刃大大2 小时前
【Vue】默认插槽 && 具名插槽 && 作用域插槽
前端·javascript·vue.js
风之舞_yjf2 小时前
Vue基础(27)_脚手架安装
vue.js
BYSJMG3 小时前
计算机毕设选题推荐:基于大数据的癌症数据分析与可视化系统
大数据·vue.js·python·数据挖掘·数据分析·课程设计
XPii3 小时前
Photoshop应用——将图片变为油画效果
ui·photoshop