vue3 table动态合并,自定义参数合并单元格

复制代码
<template>
    <div>
        <el-table :data="tableData" :span-method="objectSpanMethod" border
            :header-cell-style="{ textAlign: 'center'}">
            <el-table-column prop="area" label="区域" align="center">
            </el-table-column>
            <el-table-column prop="province" label="省份" align="center" />
            <el-table-column prop="month_1" label="一月" align="center" />
            <el-table-column prop="month_2" label="二月" align="center" />
            <el-table-column prop="month_3" label="三月" align="center" />
        </el-table>
    </div>
</template>

<script lang="ts" setup>
import { ref, toRefs, reactive, nextTick, computed, watch, onMounted } from "vue";
const tableData = [
        // 一年级
        {
            area: "华北",
            province: "北京",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "华北",
            province: "北京",
            month_1: "200",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "华北",
            province: "山西",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "华北",
            province: "辽宁",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "东北",
            province: "吉林",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "东北",
            province: "黑2",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "东北",
            province: "黑2",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "东北",
            province: "黑2",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "西北",
            province: "黑2",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "西北",
            province: "西二",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
        {
            area: "西北",
            province: "西三",
            month_1: "100",
            month_2: "张三",
            month_3: "90"
        },
    ]
//存储合并单元格的开始位置
const setTabelRowSpan = (tableData:any, fieldArr:any, effectMerge = {}) => {
    let lastItem = {}
    fieldArr.forEach((field:any, index:any) => {
        let judgeArr = fieldArr.slice(0, index + 1)
        //@ts-ignore
        if (effectMerge[field]) {
            //@ts-ignore
            judgeArr = [...effectMerge[field], field]
            console.log(judgeArr, "--==judgeAr2222");

        }
        tableData.forEach((item:any) => {
            item.mergeCell = fieldArr
            const rowSpan = `rowspan_${field}`
            // 判断是否合并到上个单元格。
             //@ts-ignore
            if (judgeArr.every((e:any) => lastItem[e] === item[e] && item[e] !== '')) {
                // 判断是否所在行的列对应的值全部相同,并且此列的值不为空
                // 是:合并行
                item[rowSpan] = 0
                 //@ts-ignore
                lastItem[rowSpan] += 1
            } else {
                // 否:完成一次同类合并。lastItem重新赋值,进入下一次合并计算。
                item[rowSpan] = 1
                lastItem = item
            }
        })
    })
}
 //@ts-ignore
const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
    if (row.mergeCell.includes(column.property)) {
        const rowspan = row[`rowspan_${column.property}`]
        if (rowspan) {
            return { rowspan: rowspan, colspan: 1 }
        } else {
            return { rowspan: 0, colspan: 0 }
        }
    }
}
onMounted(()=>{
    const effectMerge = {
        month_1: ['area'],//一月参照区域进行合并
        month_3: ['area']//三月参照区域合并
    }
    const arr = ['area', 'province', 'month_1', 'month_2', 'month_3']//全部字段
    setTabelRowSpan(tableData, arr, effectMerge)// (表格数据,表格字段,合并的条件)
})
</script>

<style></style>

效果图:

相关推荐
阿里巴啦19 分钟前
照片隐私清理工具:基于Taro 4 + Vue 3 + piexifjs开发实践项目
vue.js·照片隐私清除·piexifjs·exif 解析
贾修行21 分钟前
Kestrel:.NET 的高性能 Web 服务器探秘
服务器·前端·kestrel·.net·net core·web-server·asp.net-core
吃吃喝喝小朋友21 分钟前
HTML DOM
前端·javascript·html
HWL567921 分钟前
HTML中,<video> 和 <source> 标签
前端·javascript·html
球球不吃虾22 分钟前
分享一个简单的交互式塔罗牌抽牌应用
前端·vue
2501_9481201523 分钟前
中职动漫设计与制作专业实训方案研究
前端·人工智能·语言模型·自然语言处理·架构
沛沛老爹24 分钟前
从Web到AI:多模态Agent图像识别Skills开发实战——JavaScript+Python全栈图像处理方案
java·javascript·图像处理·人工智能·python·rag
小小鸟00824 分钟前
前端 RBAC基于角色的权限控制(按钮级别)
前端
学习java的小库里25 分钟前
EasyExcel复杂导出
java·前端