antdvue-Table首列合并

本文介绍如何在Vue应用中使用Ant Design Table组件实现数据源的合并列和动态列宽,通过rowSpan方法根据接口返回的数据调整表格布局。适合前端开发者理解和实践数据处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<a-table
                        :columns="noPmColumns"
                        :dataSource="sourceLeftData"
                        :loading="tableLoading"
                        :scroll="{  }"
                        rowKey="rowKeyInfo"
                        :showHeader="false"
                        :pagination="false"
                        size="middle"
                        bordered
                    >
                    </a-table>
noPmColumns:[
                {
                    title:"mpdGroup",
                    dataIndex:"mpdGroup",
                    width: 127,
                    align: "center",
                    customRender:(value, row, index)=>{
                        const obj = {
                            children: value,
                            attrs: {},
                        }; 
                        obj.attrs.rowSpan = row.mergeRowSpan;         
                        return obj
                    }
                },
                {
                    title:"eqpId",
                    dataIndex:"eqpId",
                    width: 127,
                    align: "center",
                },
                {
                    title:"chamberList",
                    dataIndex:"chamberList",
                    scopedSlots: { customRender: "action" },
                    align: "center",
                    customCell:function(record,index){
                        return {
                            style:{
                                padding:'1px !important'
                            }
                        }
                    }
                },
            ],
            sourceLeftData:[],

合并第一列的方法:
在请求到接口的list数据后,调用rowSpan方法

//getData代表请求过来的list
//dataIndex代表每一行的第一列的key名称
rowSpan(getData,dataIndex) {
            //过滤合并后现实的表格内容
            let arr = getData
                .reduce((result, item) => {
                //不存在则放入展示结果
                if (result.indexOf(item[dataIndex]) < 0) {
                    result.push(item[dataIndex]);
                }
                return result;
                }, [])
                //确定每个合并的合并行数
                .reduce((result, keys) => {
                    //计算出可以合并的数据
                    const children = getData.filter((item) => item[dataIndex] === keys);
                    result = result.concat(
                    //计算出合并行数
                        children.map((item, index) => ({
                        ...item,
                        [`mergeRowSpan`]: index === 0 ? children.length : 0,
                        }))
                    );
                    return result;
                }, []);
                console.log("arr",arr);
                //将最后展示的数据替换原数据
            this.sourceLeftData = arr;
            
        },
Ant Design Vue (Antd-Vue) 的表格组件 Table 提供了一个自定义渲染功能 `customRender` 或 `render` 属性,用于动态合并单元格,特别是当需要对数据和列进行动态处理的时候。`customRender`允许你完全控制每个单元格的内容生成。 例如,如果你的数据结构包含嵌套的对象,或者某些列的值需要基于其他列计算得出,你可以编写一个函数接收当前行的数据、列配置以及索引等参数,然后返回一个 HTML 结构。以下是一个简单的示例: ```javascript <template> <a-table :data="tableData" :columns="tableColumns" :rowKey="getRowKey"> <template #cell="{ row, column, $index }"> <!-- 使用 customRender 进行动态合并 --> <span v-if="column.key === 'mergeColumn'" slot-scope="scope"> {{ customRender(row, column, scope.$index) }} </span> </template> </a-table> </template> <script> export default { data() { return { tableData: [ // ... 数据示例 ], tableColumns: [ { key: 'staticColumn', title: '静态列' }, { key: 'mergeColumn', title: '合并列', render: this.customRender }, // ... 其他列 ], }; }, methods: { getRowKey(item) { // 返回唯一标识,防止表格重复渲染 return item.id; }, customRender(row, column, index) { // 根据实际需求编写合并逻辑 if (/* 判断条件 */) { // 可能会合并多个单元格,这里可以拼接字符串或其他操作 return `${row.subData[index] || ''} ${row.anotherData[index]}`; } return row[column.key]; }, }, }; </script> ``` 在这个例子中,`customRender` 函数可以根据每行和每列的具体情况生成内容,并且由于支持列的动态设置,你还可以根据列配置决定是否合并及如何合并
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值