• element plus 树形 横向 合并 table表格


    <el-table class="w100" height="100%" :span-method="spanMethod" :data="state.tableTemData" :cell-style="cellStyle" border :show-header="false">
      <el-table-column align="center" prop="name" label="" />
      <el-table-column align="center" prop="name2" label="时间" />
      <el-table-column align="center" prop="name3" label="" />
      <el-table-column align="center" prop="name4" label="8:00" />
       <el-table-column align="center" prop="name5" label="1111" />
       <el-table-column align="center" prop="name5" label="1111" />
       <el-table-column align="center" prop="name5" label="1111" />
       <el-table-column align="center" prop="name5" label="1111" />
    </el-table>
    
    <script setup>
    import { reactive } from 'vue-demi'
    const state = reactive({
    //所需数据格式
    tableData: [
        { name: '集控中心', name2: '器器器1', name3: '电压A', name4: '8:00', name5: '1111', rowspan_0: 11, rowspan_1: 4,rowspan_2: 1 },
        { name: '集控中心', name2: '器器器1', name3: '电压B', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 0, rowspan_2: 1 },
        { name: '集控中心', name2: '器器器1', name3: '电压C', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 0, rowspan_2: 1 },
        { name: '集控中心', name2: '器器器1', name3: '电压D', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 0, rowspan_2: 1 },
        { name: '集控中心', name2: '器器器2', name3: '电压E', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 2, rowspan_2: 1 },
        { name: '集控中心', name2: '器器器2', name3: '电压F', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 0, rowspan_2: 1 },
        { name: '集控中心', name2: '器器器3', name3: '电压G', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 3, rowspan_2: 1 },
        { name: '集控中心', name2: '器器器3', name3: '电压H', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 0, rowspan_2: 1 },
        { name: '集控中心', name2: '器器器3', name3: '电压I', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 0, rowspan_2: 2 },
        { name: '集控中心', name2: '器器器4', name3: '电压J', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 2, rowspan_2: 0 },
        { name: '集控中心', name2: '器器器4', name3: '电压K', name4: '8:00', name5: '1111', rowspan_0: 0, rowspan_1: 0, rowspan_2: 1 },
      ],
    //原数据格式
      treeData: [
        {
          name: '集控中心',
          children: [
            {
              name2: '时间',
              children: [{ name3: '', name4: '8:00', name5: '1111' }]
            },
            {
              name2: '器器器1',
              children: [
                { name3: '电压A', name4: '8:00', name5: '1111' },
                { name3: '电压B', name4: '8:00', name5: '1111' }
              ]
            },
            {
              name2: '器器器2',
              children: [
                { name3: '电压A', name4: '8:00', name5: '1111' },
                { name3: '电压B', name4: '8:00', name5: '1111' }
              ]
            },
            {
              name2: '器器器3',
              children: [
                {
                  name3: '电压A',
                  children: [
                    { name4: '8:00', name5: '1111' },
                    { name4: '8:00', name5: '1111' }
                  ]
                },
                {
                  name3: '电压B',
                  children: [
                    { name4: '8:00', name5: '1111' },
                    { name4: '8:00', name5: '1111' }
                  ]
                },
                { name3: '电压C', name4: '8:00', name5: '1111' }
              ]
            },
            {
              name2: '器器器4',
              children: [
                { name3: '电压A', name4: '8:00', name5: '1111' },
                { name3: '电压B', name4: '8:00', name5: '1111' },
                { name3: '电压B', name4: '8:00', name5: '1111' },
                
              ]
            },
          ]
        }
      ],
      count: 0,
      tableTemData: [],
      maxArr: []
    })
    
    function tree_To_table(arr, num) {
      arr.forEach((item, index) => {
        //   初始化数量
        state.count = 0
        if (item.children) {
          // 计算所有所有子集数量
          getnum(item.children)
          item['rowspan_' + num] = state.count
          //   深复制后删除children,否则可能会造成死循环
          let itemVar = JSON.parse(JSON.stringify(item))
          delete itemVar.children
          item.children.forEach((m, n) => {
            //   将除第一行之外的重置为0
            if (n != 0) {
              for (let o = 0; o <= num; o++) {
                itemVar['rowspan_' + o] = 0
              }
            }
            // 合并父子级
            Object.assign(m, itemVar)
          })
          tree_To_table(item.children, num + 1)
        } else {
          item['rowspan_' + num] = 1
          state.maxArr.push(num)
          state.tableTemData.push(item)
        }
      })
    }
    tree_To_table(state.treeData)
    function getnum(arr) {
      arr.forEach((item, index) => {
        if (item.children) {
          getnum(item.children)
        } else {
          state.count += 1
        }
      })
    }
    const spanMethod = ({ row, column, rowIndex, columnIndex }) => {
      let index = state.tableTemData.indexOf(row)
      if (columnIndex < Math.max(...state.maxArr)) {
        let rowspan = row['rowspan_' + columnIndex]
        if (rowIndex == 0 && columnIndex == 0) {
          rowspan = state.tableTemData.length
        }
        return {
          rowspan,
          colspan: rowspan == 0 ? 0 : 1
        }
      }
      return {
        rowspan: 1,
        colspan: 1
      }
    }
    function cellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex < Math.max(...state.maxArr) || rowIndex == 0) {
        return 'background:rgba(50, 247, 255, 0.08)'
      }
    }
    </script>
    
  • 相关阅读:
    【codevs1690】开关灯 (线段树 区间修改+区间求和 (标记))
    【codevs1191】数轴染色 (线段树 区间修改+固定区间查询)
    【机器学习】李航 统计学习方法 知识点总结
    【机器学习】生成模型 判别模型
    【机器学习】贝叶斯定理、精准推断、最大似然估计、连续特征参数估计、EM算法
    python queue 讲解
    【tensorflow】 CNN卷积神经网络原理讲解+图片识别应用(附源码)
    URL解析过程
    Python 可迭代对象迭代器生成器的区别
    【Linux】 修改主机名
  • 原文地址:https://www.cnblogs.com/FormerWhite/p/15879661.html
Copyright © 2020-2023  润新知