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>
相关推荐
小蹦跶儿13 分钟前
Vue项目中字体引入:完整实操指南
vue.js·字体·视觉设计
遇见很ok33 分钟前
Web Worker
前端·javascript·vue.js
前端不太难38 分钟前
RN Navigation vs Vue Router:从架构底层到工程实践的深度对比
javascript·vue.js·架构
天问一1 小时前
前端Vue使用js-audio-plugin实现录音功能
前端·javascript·vue.js
小魏的马仔1 小时前
【elementui】el-date-picker日期选择框,获取焦点后宽度增加问题调整
前端·vue.js·elementui
ttod_qzstudio2 小时前
事件冒泡踩坑记:一个TDesign Checkbox引发的思考
前端·javascript·vue.js·tdesign
悟能不能悟2 小时前
vue导出excel文件
前端·vue.js·excel
VX:Fegn08952 小时前
计算机毕业设计|基于springboot + vue电影院购票管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
q_19132846952 小时前
基于SpringBoot2+Vue2的企业合作与活动管理平台
java·vue.js·经验分享·spring boot·笔记·mysql·计算机毕业设计
前端白袍2 小时前
Vue:如何实现日志导出下载功能?
javascript·vue.js·ecmascript