elementUI中el-table组件列样式动态控制

elementUI中el-table组件动态控制列样式

需要实现效果:根据列日期值和当前日期判断显示不同的颜色,需要计算列的逻辑然后动态赋值实现列样式的动态控制。

方案1

使用 scoped slot (作用域插槽)来实现动态显示
实现效果
在这里插入图片描述
代码:

<el-table-column  align="center" prop="deliveryTime"  label="合同/订单履约交付日期"  min-width="180">
  <template slot-scope="scope">
    <span :style="{ background: getDateColor(scope.row.deliveryTime), color: '#000000' }">
      {{scope.row.deliveryTime}}
    </span>
  </template>
</el-table-column>


// 自定义方法来判断并返回颜色
getDateColor (dateString) {
  const date = new Date(dateString)
  const today = new Date()
  const future = new Date()
  future.setDate(future.getDate() + 15)
  if (date > future) {
    return '#00FF00' // 绿色
  } else if (today > date) {
    return '#B22222' // 红色
  } else {
    return '#FFA500' // 黄色
  }
},

方案2

使用 cell-style设置单元格样式
实现效果:
在这里插入图片描述
代码

<el-table
  :data="dataList" border :max-height="tableHeight"
  :height="tableHeight"
  :row-style="{height:'20px'}"
  ref="dataTable"
  :cell-style="cellStyle"
  style="width: 100%;">


// 表格列样式控制
cellStyle ({row, column, rowIndex, columnIndex}) {
  if (column.property === 'deliveryTime') {
    const date = new Date(row.deliveryTime)
    const today = new Date()
    const future = new Date()
    future.setDate(future.getDate() + 15)
    if (date > future) {
      return {
        background: '#00FF00',
        color: '#000000'}
    } else if (today > date) {
      return {
        background: '#B22222',
        color: '#000000'}
    } else {
      return {
        background: '#FFA500',
        color: '#000000'}
    }
  }
},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值