el-table给相关行添加背景色(tree数据结构)

在Vue.js项目中,使用Element UI的el-table组件时,当表格数据为树形结构时,通过row-class-name绑定tableRowClassName来添加行背景色。需要注意,在tree数据结构下,rowIndex不再适用。要使row-class-name、row-style、cell-class-name生效,需使用全局CSS类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">

el-table添加row-class-name绑定tableRowClassName

在table数据不是tree数据时,rowIndex拿到的是table行的索引

tableRowClassName({ row,rowIndex }) {
       if (rowIndex % 2 === 0) {
    		return "";
       } else {
			return "rowStyle"
       }
   },


<style lang="less">
.el-table .rowStyle {
  background-color: #F8F8F8;
}
</style>

在table数据是tree数据结构时:

<el-table
          ref="multipleTable"
          :data="classifyData"
          row-key="id"
          default-expand-all
          :tree-props="{ children: 'chlidList', hasChildren: 'hasChildren' }"
          tooltip-effect="dark"
          style="width: 100%;"
          @select="select"
          @select-all="selectAll"
          @selection-change="handleSelectionChange"
          :row-class-name="classifyTableRow"
          border
        ></el-table>

rowIndex拿到的是classifyData包含chlidList的索引
解决:对源数据classifyData进行处理,添加index

this.classifyData.forEach((item,index) => {
      item.index = index + 1
      if(item.chlidList && item.chlidList.length > 0) {
            item.chlidList.forEach(i => {
               i.index = index + 1
            })
        }
})

classifyTableRow({ row, rowIndex }) {
      if(row.index % 2 === 0) {
          return ''
        } else {
          return 'rowStyle'
        }
 },

在elementUI中,row-class-name、row-style、cell-class-name等属性要想生效必须使用全局class才能生效,所以不能放在里面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值