您的位置:首页 > 游戏 > 手游 > table根据字段合并单元格

table根据字段合并单元格

2024/12/25 22:23:31 来源:https://blog.csdn.net/weixin_44255044/article/details/139598615  浏览:    关键词:table根据字段合并单元格

table根据字段合并单元格

全局 tableSpanUtils.ts

export const createObjectSpanMethod = (fields: string[], tableData: any) => {return ({ row, column, rowIndex, columnIndex }: any) => {const prop = column.propertyconst calculateRowspan = (field: any, rowIndex: any) => {let rowspan = 1for (let i = rowIndex + 1; i < tableData.length; i++) {if (tableData[i][field] === tableData[rowIndex][field]) {rowspan++} else {break}}return rowspan}const checkPreviousRow = (field: any, rowIndex: any) => {if (rowIndex > 0 && tableData[rowIndex][field] === tableData[rowIndex - 1][field]) {return true}return false}//序号单独处理if (prop === 'key') {const allFieldsMatch = fields.every((field) => checkPreviousRow(field, rowIndex))if (allFieldsMatch) {return { rowspan: 0, colspan: 0 }} else {const minRowspan = fields.reduce((min, field) => {const rowspan = calculateRowspan(field, rowIndex)return Math.min(min, rowspan)}, tableData.length - rowIndex)return { rowspan: minRowspan, colspan: 1 }}}if (fields.includes(prop)) {if (checkPreviousRow(prop, rowIndex)) {return { rowspan: 0, colspan: 0 }}return { rowspan: calculateRowspan(prop, rowIndex), colspan: 1 }}return { rowspan: 1, colspan: 1 }}
}

使用方法

// 引入 createObjectSpanMethod 
import { createObjectSpanMethod } from '@/utils/tableSpanUtils'// 和 prop字短对应合并 
const fields: string[] = ['merchant_name','card_status','type'
] 
// html
<el-table :span-method="createObjectSpanMethod(fields, tableData)" class="defualtTable" :data="tableData"></el-table>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com