跳到主要内容
版本:Next

合并行相关

合并行函数

/* 
data:处理的数据
name:合并标记的字段
link:其他相关字段,可多个参数
*/
function setListRowSpan(source, name, ...link) {
const setRowSpan = (source, name, ...link) => {
let linkHead = null;
if (link && link.length > 0) {
linkHead = link[0];
}
let data = JSON.parse(JSON.stringify(source));
const rowSpan = `${name}RowSpan`;
let titleRow = data[0]; // 合并行
titleRow[rowSpan] = 1;
data.forEach((row, index) => {
if (linkHead) row[name + linkHead] = row[name] + "-" + row[linkHead];
const nextRow = data[index + 1];
if (!nextRow) return;
if (row[name] === nextRow[name]) {
titleRow[rowSpan] += 1;
nextRow[rowSpan] = 0;
} else {
titleRow = nextRow; // 修改指向
titleRow[rowSpan] = 1;
}
});
// console.log(data)
if (linkHead && link.length > 1) {
data = setRowSpan(data, name + linkHead, link.slice(1));
}
return data;
};

const dataList = setRowSpan(source, name, ...link);
// 合并最后一次的数据
const res = setRowSpan(dataList, [name, ...link].join(""));
return res;
}

column 使用

// 简单使用
const data=[{},···]

setRowSpan(data,'title','name') //改变原数据对象

const columns = [
{
title: "风险类别",
dataIndex: "title",
render: (text, row) => {
return {
children: text,
props: {
rowSpan: row.titleRowSpan, // 使用
},
};
},
},
];