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>
相关推荐
左手吻左脸。3 小时前
解决el-select因为弹出层层级问题,不展示下拉选
javascript·vue.js·elementui
李白的故乡3 小时前
el-tree-select名字
javascript·vue.js·ecmascript
Rysxt_3 小时前
Element Plus 入门教程:从零开始构建 Vue 3 界面
前端·javascript·vue.js
隐含3 小时前
对于el-table中自定义表头中添加el-popover会弹出两个的解决方案,分别针对固定列和非固定列来隐藏最后一个浮框。
前端·javascript·vue.js
90后的晨仔5 小时前
Vue中为什么要有 Provide / Inject?
前端·vue.js
f 查看所有勋章6 小时前
六轴工业机器人可视化模拟平台 (Vue + Three.js + Blender)
javascript·vue.js·机器人
我的xiaodoujiao6 小时前
从 0 到 1 搭建 Python 语言 Web UI自动化测试学习系列 8--基础知识 4--常用函数 2
前端·python·测试工具·ui
我命由我123458 小时前
Photoshop - Photoshop 工具栏(10)透视裁剪工具
经验分享·笔记·学习·ui·职场和发展·职场发展·photoshop
ziyue75758 小时前
vue修改element-ui的默认的class
前端·vue.js·ui