横、列动态渲染,含有二级分类的 el-table 表格
在 vue3 + element plus 中实现复杂的 el-table 表格功能,如横、列动态渲染,含有二级分类,具体包含以下步骤:
html 代码:
<el-table :span-method="arrayspanmethod" :data="tabledata"> <el-table-column label="派出组" align="center" prop="group"></el-table-column> <el-table-column label="累计生产单" align="center" prop="total"></el-table-column> <el-table-column label="案件主办" align="center" prop="person"></el-table-column> <el-table-column v-for="(item, index) in headerlist" :key="index" :label="item.label" align="center" :prop="item.prop" > <el-table-column v-for="(list, i) in item.sublist" :key="i" :label="list.label" align="center" :prop="list.prop" > <template #default="scope">{{ scope.row[item.prop][list.prop] }}</template> </el-table-column> </el-table-column> </el-table>
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 表格功能,如横、列动态渲染,含有二级分类,并支持合并行和列?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号