Vue el-table 合并单元格

5 篇文章 0 订阅

一般常见的就是下图这种的单列,上下重复进行合并。
在这里插入图片描述
有时候可能也会需要多行多列的合并。
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./assets/global.css">

    <!-- 引入样式 -->
    <link rel="stylesheet" href="./assets/element-ui/lib/theme-chalk/index.css">
</head>

<body>
    <div id="app">
        <el-table :data="tableData" :span-method="handleSpanMethod" border style="width: 100%">
            <el-table-column prop="id" label="ID" width="180">
            </el-table-column>
            <el-table-column prop="name" label="姓名">
            </el-table-column>
            <el-table-column prop="amount1" sortable label="数值 1">
            </el-table-column>
            <el-table-column prop="amount2" sortable label="数值 2">
            </el-table-column>
            <el-table-column prop="amount3" sortable label="数值 3">
            </el-table-column>
        </el-table>

    </div>
</body>
<!-- import Vue before Element -->
<script src="./assets/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="./assets/element-ui/lib/index.js"></script>

<script>

    new Vue({
        el: '#app',
        data: function () {
            return {
                tableData: [{
                    id: '12987122',
                    name: '王小虎',
                    amount1: '234',
                    amount2: '3.2',
                    amount3: 10
                }, {
                    id: '12987123',
                    name: '王小虎',
                    amount1: '165',
                    amount2: '4.43',
                    amount3: 12
                }, {
                    id: '12987124',
                    name: '王小丫',
                    amount1: '324',
                    amount2: '1.9',
                    amount3: 9
                }, {
                    id: '12987125',
                    name: '王小丫',
                    amount1: '621',
                    amount2: '2.2',
                    amount3: 17
                }, {
                    id: '12987126',
                    name: '王小丫',
                    amount1: '539',
                    amount2: '4.1',
                    amount3: 15
                }]
            }
        },
        mounted() {
        },
        methods: {
            handleSpanMethod({ row, column, rowIndex, columnIndex }) {
                // console.log(this.mergeByName(this.tableData, 1));
                // return this.mergeByRectangle(0, 0, 0, 2)(columnIndex, rowIndex)
                // return this.mergeByRectangleList([[0, 0, 0, 1], [2, 0, 2, 1], [3, 3, 4, 4]])(columnIndex, rowIndex)
                return this.mergeByRectangleList(this.mergeByName(this.tableData, 1))(columnIndex, rowIndex)
            },
            /* 矩形合并单元格子
             * (sx,sy) 开始坐标
             * (ex,ey) 结束坐标
             */
            mergeByRectangle(sx, sy, ex, ey) {
                return function (x, y) {
                    if (x == sx && y == sy) {
                        return {
                            rowspan: (ey - sy) + 1,
                            colspan: (ex - sx) + 1
                        }
                    }
                    if (sx <= x && x <= ex && sy <= y && y <= ey) {
                        return {
                            rowspan: 0,
                            colspan: 0
                        }
                    }
                }
            },
            /* 矩形合并单元格
             * @param {[[]]} arraylist
             */
            mergeByRectangleList(arraylist) {
                const that = this;
                return function (x, y) {
                    for (let i = 0; i < arraylist.length; i++) {
                        const arr = arraylist[i];
                        const res = that.mergeByRectangle(...arr)(x, y)
                        if (res) return res
                    }
                }
            },
            /* 通过名称计算合并单元格
             */
            mergeByName(tableData, column) {
                let arraylist = []
                let sx = column;
                let sy = 0;
                let ex = column;
                let ey = 0;
                let c = 0;
                let i = 1;

                while (i < tableData.length) {
                    const curr = tableData[c];
                    const next = tableData[i]
                    if (curr.name == next.name) {
                        ey++;
                    }

                    if (curr.name != next.name || i == tableData.length - 1) {
                        arraylist.push([sx, sy, ex, ey])
                        c = i
                        sy = c
                        ey = c
                    }
                    i++;
                }
                return arraylist
            }

        }
    })


</script>

</html>

在线预览
https://linyisonger.github.io/H5.Examples/
源码仓库
https://github.com/linyisonger/H5.Examples.git

  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Vue3中使用Element Plus的表格组件合并单元格,你可以按照以下步骤进行操作: 1. 首先,确保你已经正确安装和配置了Vue3和Element Plus。 2. 在你的组件中引入Element Plus的Table组件和其他必要的模块: ```javascript import { ElTable, ElTableColumn } from 'element-plus'; ``` 3. 在模板中使用Table组件,并定义表格的数据源和列属性: ```html <template> <el-table :data="tableData"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> </el-table> </template> ``` 4. 在数据源中,使用`span-method`属性来定义合并单元格的逻辑。这个属性接受一个方法,该方法可以根据当前行和列的索引来决定是否需要合并单元格: ```html <template> <el-table :data="tableData"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄" :span-method="handleSpanMethod"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20 }, { name: '李四', age: 22 }, { name: '王五', age: 22 }, { name: '赵六', age: 25 } ] }; }, methods: { handleSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { if (rowIndex === 1 || rowIndex === 2) { return { rowspan: 2, colspan: 1 }; } } } } }; </script> ``` 在上面的例子中,我们使用`handleSpanMethod`方法来判断是否需要合并单元格。如果列索引为0,并且行索引为1或2,我们返回一个包含`rowspan`和`colspan`的对象,指定要合并的行数和列数。 这样就可以实现在Vue3中使用Element Plus的表格组件合并单元格了。你可以根据自己的需求进行适当调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林一怂儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值