登录
首页 >  文章 >  前端

如何在 Vue3 + Element Plus 中实现复杂的 el-table 表格功能,如横、列动态渲染,含有二级分类,并支持合并行和列?

时间:2024-10-27 15:03:57 106浏览 收藏

哈喽!今天心血来潮给大家带来了《如何在 Vue3 + Element Plus 中实现复杂的 el-table 表格功能,如横、列动态渲染,含有二级分类,并支持合并行和列?》,想必大家应该对文章都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习文章,千万别错过这篇文章~希望能帮助到你!

如何在 Vue3 + Element Plus 中实现复杂的 el-table 表格功能,如横、列动态渲染,含有二级分类,并支持合并行和列?

横、列动态渲染,含有二级分类的 el-table 表格

在 vue3 + element plus 中实现复杂的 el-table 表格功能,如横、列动态渲染,含有二级分类,具体包含以下步骤:

html 代码:


  
  
  
  
    
      
    
  

js 代码:

// 模拟后台返回的表格头部信息
const headerList = [
  {
    label: '达飞',
    prop: 'column1',
    subList: [
      {
        label: '保理当日',
        prop: 'day'
      },
      {
        label: '保理累计',
        prop: 'total'
      }
    ]
  },
  {
    label: '农行',
    prop: 'column2',
    subList: [
      {
        label: '农行秦分当日',
        prop: 'day'
      },
      {
        label: '农行秦分累计',
        prop: 'total'
      }
    ]
  }
];

// 模拟后台返回的表格数据信息
const tableData = [
  {
    group: '海港组',
    total: 123,
    person: 'aa',
    column1: {
      day: '500',
      total: 3000
    },
    column2: { day: 100, total: 3000 }
  },
  {
    group: '海港组',
    total: 123,
    person: 'bb',
    column1: {
      day: '500',
      total: 3000
    },
    column2: { day: 100, total: 3000 }
  },
  {
    group: '海港组',
    total: 123,
    person: 'cc',
    column1: {
      day: '500',
      total: 3000
    },
    column2: { day: 100, total: 3000 }
  },
  {
    group: '其他组',
    total: 123,
    person: 'cc',
    column1: {
      day: '500',
      total: 3000
    },
    column2: { day: 100, total: 3000 }
  },
  {
    group: '其他组',
    total: 123,
    person: 'cc',
    column1: {
      day: '500',
      total: 3000
    },
    column2: { day: 100, total: 3000 }
  },
  {
    group: '其他组',
    total: 234,
    person: 'cc',
    column1: {
      day: '500',
      total: 3000
    },
    column2: { day: 100, total: 3000 }
  }
];

// 用于合并组
const groupArr = [];

// 用于合并生产单
const totalArr = [];

// 表格行合并方法
const merge = (tableData) => {
  for (var i = 0; i < tableData.length; i++) {
    if (i === 0) {
      // 第一行必须存在
      groupArr.push(1);
      totalArr.push(1);
    } else {
      // 判断当前元素与上一个元素是否相同
      if (tableData[i].group === tableData[i - 1].group) {
        groupArr[groupArr.length - 1] += 1;
      } else {
        groupArr.push(1);
      }

      // 生产单合并
      if (
        tableData[i].total === tableData[i - 1].total &&
        tableData[i].group === tableData[i - 1].group
      ) {
        totalArr[totalArr.length - 1] += 1;
      } else {
        totalArr.push(1);
      }
    }
  }
};

// 合并行
const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    // 合并组
    const _row_1 = groupArr[rowIndex];
    const _col_1 = _row_1 > 0 ? 1 : 0;
    return {
      rowspan: _row_1,
      colspan: _col_1
    };
  } else if (columnIndex === 1) {
    // 合并生产单
    const _row_2 = totalArr[rowIndex];
    const _col_2 = _row_2 > 0 ? 1 : 0;
    return {
      rowspan: _row_2,
      colspan: _col_2
    };
  }
};

merge(tableData.value);

此代码可实现根据动态数据渲染表格,横、列均为动态,且支持二级分类的合并和展示。

今天关于《如何在 Vue3 + Element Plus 中实现复杂的 el-table 表格功能,如横、列动态渲染,含有二级分类,并支持合并行和列?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>