el-table父子结构数据样式控制

el-table父子结构数据样式控制

实现效果:,
使用el-table组件展示具有父子结构的数据,控制父子样式一致,父级之间样式不同的效果,实现友好的用户交互功能。
在这里插入图片描述
注意点:
1.父子结构数据表单有时候会出现展开箭头失效问题,一般由于样式问题;
2.如果要控制父子样式相同,间隔父级样式不同需要在原始数据结构中进行处理。
代码实现:

// 接收树形结构的表格设置::tree-props="{children: 'children', hasChildren: 'hasChildren'}"
// 根据数据动态控制表格样式:使用动态样式控制
<el-table
  :data="dataList"
  row-key="id"
  ref="dataTreeList"
  border
  :cell-style="{padding:'1px 0'}" style="width: 100%;"
  :max-height="tableHeight" :height="tableHeight"
  :default-expand-all="isExpansion" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
  :row-class-name="tableRowClassName">
  
 // --------------method:---------------------------------------------
  tableRowClassName (rows) {
   return rows.row.background
  },
  
 //处理原始数据-----给数据添加额外属性background,在动态样式中获取background作为样式显示
 //----根据父级所在行的奇偶性控制样式,偶数行父级样式为first-row,奇数行父级样式为second-row
 // ----子级样式与父级样式相同
  delTableStyle () {
  this.dataList.forEach((item, index) => {
    if (item.children.length > 0) {
      if (index % 2 === 0) {
        item.background = 'first-row'
        item.children.forEach((ch, ind) => {
          ch.background = 'first-row'
        })
      } else {
        item.background = 'second-row'
        item.children.forEach((ch, ind) => {
          ch.background = 'second-row'
        })
      }
    } else {
      if (index % 2 === 0) {
        item.background = 'first-row'
      } else {
        item.background = 'second-row'
      }
    }
  })
},

//------------------------------样式-------------------------
/deep/.el-table .first-row{
  background-color: #87CEFA;
}

/deep/.el-table .second-row{
  background-color: #F0F8FF;
}

// 去掉eltable的hover效果
/deep/.el-table tbody tr:hover > td {
  background-color: transparent !important;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值