Vue使用element-ui table实现多级表头设置不同背景色

Vue使用element-ui table实现多级表头设置不同背景色

目录

前提

首先,我们要了解table表头行列是怎么排序的,我画了一个简单的图标注一下,其中坐标表示(rowIndex, columnIndex),这里要特别注意红色的坐标,表示每一行的起始坐标
在这里插入图片描述

效果

以下是使用我的思路实现的效果,当然,网上也有很多大牛分享的能实现效果的帖子,这里主要是针对和我一样小白的菜狗做一个思路整理,知其所以然才能应对不同的需求
在这里插入图片描述

思路整理

1、使用header-cell-style方法实现;
2、明确我们表头渠道的顺序(表头的名称一般都是前端写死的);
红框为一级,蓝框为二级,绿框为三级
在这里插入图片描述

3、将我们的二级部门和三级部门分别提炼成一个数组(顺序无所谓);
下面是二级,上面是三级
在这里插入图片描述
4、定义一个对象,存放二级和三级部门在整个渠道数组中的索引

created() {
    this.indexList = this.getChannelIndex()
 },
 method: { 
    // 找出二级部门和三级部门排序索引值,用于设置表头样式
    getChannelIndex() {
      let indexs2 = this.channel2List.map(m => sort().channelSortNew.indexOf(m))
      let indexs3 = this.channel3List.map(m => sort().channelSortNew.indexOf(m))
      return { indexs2, indexs3 }
    }
 }

5、header-cell-style绑定方法的实现:

// 下面正式开始设置样式
      if (rowIndex === 0) { // 第一行的样式设置
        if (columnIndex === 0 || columnIndex === 1) { // 如果是第一行第一列和第二列,则设置为以下样式
          return 'background-color: #ecf1e0; color: #000;border: .5px solid #aaa;'
        } else if (columnIndex < 5) { // 如果是第一行且小于第六列(实际就是我们的一级部门,因为位置固定且靠前,所以我直接写死了),则设置为以下样式
          return 'background-color: #7c9148; color: #000;border: .5px solid #aaa;'
        } else if (indexs2.includes(columnIndex - 2)) {
          // 判断当前columnIndex是否存在于indexs2中(减2是因为我们时间段和品线这两列,它们也会算在第一行,但我们的渠道其实是从索引值2开始的)
          return 'background-color: #c8d6a1; color: #000;border: .5px solid #aaa;'
        } else if (indexs3.includes(columnIndex - 2)) {
          // 同上我们判断是否存在于indexs3中(即判断是否为三级部门)
          return 'background-color: #dae3c0; color: #000;border: .5px solid #aaa;'
        } else {
          // 第一行其他情况都不符合则为四级部门
          return 'background-color: #fff; color: #000;border: .5px solid #aaa;'
        }
      } else if (rowIndex === 1) { // 第二行的样式设置
        if (columnIndex < 9) { // 因为我们第一行的一级部门下分别有三列,所以加起来是9列
          return 'background-color: #7c9148; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex >= indexs2[i2] * 3) && (columnIndex <= indexs2[i2] * 3 + 3)) {
          // 判断当前columnIndex是否是二级部门索引(如果columnIndex大于等于渠道索引的3倍且小于等于渠道索引的3倍+3)
          return 'background-color: #c8d6a1; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex >= indexs3[i3] * 3) && (columnIndex <= indexs3[i3] * 3 + 3)) {
          // 判断当前columnIndex是否是三级部门索引
          return 'background-color: #dae3c0; color: #000;border: .5px solid #aaa;'
        } else {
          // 第二行其他情况都不符合则为四级部门
          return 'background-color: #fff; color: #000;border: .5px solid #aaa;'
        }
      }
    }

复杂案例代码分享

以上步骤是简化版,明白了其中的道理,我们就可以实现更复杂的样式了,下面分享一下我一个比较复杂的实际需求涉及的完整代码

<template>
  <div class="app-container" style="margin-bottom: 40px">
    <el-row>
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        <div>
          <el-table
            id="table"
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            max-height="600"
            size="costom"
            :header-cell-style="renderHeader"
          >
            <el-table-column key="dateRange" prop="dateRange" show-overflow-tooltip fixed label="时间段" align="center" width="100" />
            <el-table-column key="product" prop="product" show-overflow-tooltip fixed label="品线" align="center" width="120" />
            <el-table-column :key="Math.random()" label="有效销售" align="center">
              <el-table-column v-for="(item, index) in channelList" :key="index" :label="item" align="center">
                <el-table-column :key="Math.random()" label="2023" align="center" />
                <el-table-column :key="Math.random()" label="2024" align="center" />
                <el-table-column :key="Math.random()" label="同比" align="center" />
              </el-table-column>
            </el-table-column>
            <el-table-column v-for="(item, index) in channelList" :key="index" :label="item" align="center">
              <el-table-column :key="Math.random()" label="整体均价" align="center">
                <el-table-column :key="Math.random()" label="2023" align="center" />
                <el-table-column :key="Math.random()" label="2024" align="center" />
                <el-table-column :key="Math.random()" label="同比" align="center" />
              </el-table-column>
              <el-table-column :key="Math.random()" label="核心品类均价" align="center">
                <el-table-column :key="Math.random()" label="2023" align="center" />
                <el-table-column :key="Math.random()" label="2024" align="center" />
                <el-table-column :key="Math.random()" label="同比" align="center" />
              </el-table-column>
            </el-table-column>
          </el-table>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>

import { getToken } from '@/utils/auth'

import { initDatawork } from '@/api/data'
import { parseTime } from '@/utils/index'
import FileSaver from 'file-saver'
import * as XLSX from 'xlsx'
import { sort, getChannelsNew } from '../../../../utils/customIndex-setting'

export default {
  name: 'ChanSalesAvePrice',
  data() {
    return {
      channelList: [], // 排好序的完整渠道列表
      channel3List: ['京东自营小计', '销售管理中心', '天猫旗舰店小计', // ....],
      channel2List: ['京东业务部', '销售管理中心', '淘系业务部',// ...],
      indexList: {}
    }
  },
  created() {
    getChannelsNew().then(res => {
      this.sum = res.channels
      this.branchs = res.normalChannels
    })
    this.indexList = this.getChannelIndex()
    this.channelList = sort().channelSortNew
  },
  methods: {
    // 表头字体样式
    renderHeader({ row, column, rowIndex, columnIndex }) { // 这里有个本人踩过的坑分享一下:rowIndex, columnIndex只能这么写,不能自己写别名
      const { indexs2, indexs3 } = this.indexList
      const len = sort().channelSortNew.length
      let i2, i3
      // 若当前表头单元格为第三行,则判断对应第二行的columnIndex是否存在于需要设置样式的数组中,是的话将数组对应值的索引保存

      if (rowIndex === 1) {
        if (columnIndex > len) {
          if (rowIndex === 1 && indexs2.includes(Math.floor((columnIndex - len) / 2))) {
            i2 = indexs2.indexOf(Math.floor((columnIndex - len) / 2))
          } else if (rowIndex === 1 && indexs3.includes(Math.floor((columnIndex - len) / 2))) {
            i3 = indexs3.indexOf(Math.floor((columnIndex - len) / 2))
          }
        }
      } else if (rowIndex === 2) {
        if (columnIndex < len * 3) {
          if (indexs2.includes(Math.floor(columnIndex / 3))) {
            i2 = indexs2.indexOf(Math.floor(columnIndex / 3))
          } else if (indexs3.includes(Math.floor(columnIndex / 3))) {
            i3 = indexs3.indexOf(Math.floor(columnIndex / 3))
          }
        } else {
          if (indexs2.includes(Math.floor((columnIndex - len * 3) / 6))) {
            i2 = indexs2.indexOf(Math.floor((columnIndex - len * 3) / 6))
          } else if (indexs3.includes(Math.floor((columnIndex - len * 3) / 6))) {
            i3 = indexs3.indexOf(Math.floor((columnIndex - len * 3) / 6))
          }
        }
      }
      if (rowIndex === 0) {
        if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2) {
          return 'background-color: #deedf2; color: #000;border: .5px solid #aaa;'
        } else if (columnIndex < 6) {
          return 'background-color: #d09996; color: #000;border: .5px solid #aaa;'
        } else if (indexs2.includes(columnIndex - 3)) {
          return 'background-color: #dfbab8; color: #000;border: .5px solid #aaa;'
        } else if (indexs3.includes(columnIndex - 3)) {
          return 'background-color: #eedddb; color: #000;border: .5px solid #aaa;'
        } else {
          return 'background-color: #fff; color: #000;border: .5px solid #aaa;'
        }
      } else if (rowIndex === 1) {
        if (columnIndex < 3) {
          return 'background-color: #64aac3; color: #000;border: .5px solid #aaa;'
        } else if (indexs2.includes(columnIndex)) {
          return 'background-color: #64aac3; color: #000;border: .5px solid #aaa;'
        } else if (indexs3.includes(columnIndex)) {
          return 'background-color: #bedde6; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex < 6 + len) && (columnIndex >= len)) {
          return 'background-color: #d09996; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex >= indexs2[i2] * 2 + len) && (columnIndex <= indexs2[i2] * 2 + len + 1)) {
          return 'background-color: #dfbab8; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex >= indexs3[i3] * 2 + len) && (columnIndex <= indexs3[i3] * 2 + len + 1)) {
          return 'background-color: #eedddb; color: #000;border: .5px solid #aaa;'
        } else {
          return 'background-color: #fff; color: #000;border: .5px solid #aaa;'
        }
      } else if (rowIndex === 2) {
        if (columnIndex < 9) {
          return 'background-color: #64aac3; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex >= indexs2[i2] * 3) && (columnIndex <= indexs2[i2] * 3 + 2)) {
          return 'background-color: #64aac3; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex >= indexs3[i3] * 3) && (columnIndex <= indexs3[i3] * 3 + 2)) {
          return 'background-color: #bedde6; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex < 18 + len * 3) && (columnIndex >= len * 3)) {
          return 'background-color: #d09996; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex >= indexs2[i2] * 6 + len * 3) && (columnIndex < indexs2[i2] * 6 + len * 3 + 6)) {
          return 'background-color: #dfbab8; color: #000;border: .5px solid #aaa;'
        } else if ((columnIndex >= indexs3[i3] * 6 + len * 3) && (columnIndex < indexs3[i3] * 6 + len * 3 + 6)) {
          return 'background-color: #eedddb; color: #000;border: .5px solid #aaa;'
        } else {
          return 'background-color: #fff; color: #000;border: .5px solid #aaa;'
        }
      }
    },
    // 找出二级部门和三级部门排序索引值,用于设置表头样式
    getChannelIndex() {
      let indexs2 = this.channel2List.map(m => sort().channelSortNew.indexOf(m))
      let indexs3 = this.channel3List.map(m => sort().channelSortNew.indexOf(m))
      console.log(indexs2, indexs3)
      return { indexs2, indexs3 }
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep #average-price {
  th{
    .cell{
      text-align: center;
    }
  }
}
</style>

下面是用以上代码实现的一个表头效果图,比较长就放两个截图
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值