
横、列动态渲染,含有二级分类的 el-table 表格
在 vue3 + element plus 中实现复杂的 el-table 表格功能,如横、列动态渲染,含有二级分类,具体包含以下步骤:
html 代码:
{{ scope.row[item.prop][list.prop] }}
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);此代码可实现根据动态数据渲染表格,横、列均为动态,且支持二级分类的合并和展示。










