当前位置:   article > 正文

el-table单元格背景色动态改变_vue eltable 单元格动态颜色

vue eltable 单元格动态颜色

首先在el-table加上这个 :cell-style=“tableCellStyle”

<el-table :data="turnoutList" border :height="300" :cell-style="tableCellStyle">
	<el-table-column label="设备名称" width="110px" align="center" key="devName" prop="devName" fixed
                         sortable :show-overflow-tooltip="true">
        </el-table-column>
 </el-table>
  • 1
  • 2
  • 3
  • 4
  • 5

然后是js方法

<script setup>
//这里是需要动态改变背景色的单元格,键是label,值是prop
const fieldMappings = {
  '设备名称': 'devName',
  '灯光配置': 'lightNum',
};

//动态改变单元格背景色
function tableCellStyle({ row, column, rowIndex, columnIndex }) {
  const fieldLabel = column.label;
  const fieldName = fieldMappings[fieldLabel];

  if (fieldName && (row[fieldName] === undefined || row[fieldName] === '')) {
    return { 'background-color': 'red' };
  }
}
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

效果如下,第一行有值,第二行没值背景色为红色
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/833437
推荐阅读
相关标签
  

闽ICP备14008679号