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>
相关推荐
前端Hardy1 小时前
干掉 Virtual DOM?尤雨溪开始"强推" Vapor Mode?
vue.js·vue-router
Mr_li1 小时前
给 Vue 开发者的 uni-app 快速指南
vue.js·uni-app
icebreaker4 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker4 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
wuhen_n5 小时前
代码生成:从AST到render函数
前端·javascript·vue.js
wuhen_n5 小时前
AST转换:静态提升与补丁标志
前端·javascript·vue.js
destinying5 小时前
性能优化之实战指南:让你的 Vue 应⽤跑得飞起
前端·javascript·vue.js
徐小夕6 小时前
JitWord Office预览引擎:如何用Vue3+Node.js打造丝滑的PDF/Excel/PPT嵌入方案
前端·vue.js·github
SuperEugene8 小时前
后台权限与菜单渲染:基于路由和后端返回的几种实现方式
前端·javascript·vue.js