排序
/**
* 排序
* @param data
* @param classifyRule
* @returns
*/
const classifyRows = (data: any[], classifyRule: any) => {
const keys = classifyRule
const classified = data.slice(1).reduce((ordered, row) => {
// 从已经排好序的行中寻找一行 row1,如果这行 row1 的 keys 值分别等于当前遍历到的行的 keys 值,就把当前遍历到的这一行插入到 row1 之后
// 否则把当前遍历到的行放在所有排好序的行的最后一行
const index: any = ordered.findIndex((orderedRow: Record<string, any>) =>
keys.reduce(
(boolean: any, curKey: string | number) => boolean && orderedRow[curKey] === row[curKey],
true
)
)
if (index !== -1) {
return [...ordered.slice(0, index + 1), row, ...ordered.slice(index + 1)]
} else { return [...ordered, row] }
}, [data[0]])
return classified
};
合并相同行
/**
* 合并相同行
* @param rows
* @param key
* @returns
*/
const mergeRows = (rows: any[], key: string) => {
rows[0].rowSpan = 1;
let idx = 0;
return rows.slice(1).reduce(
(mergedRows, item, index) => {
if (item[key] === mergedRows[idx][key]) {
mergedRows[idx].rowSpan++;
item.colSpan = 0;
} else {
item.rowSpan = 1;
idx = index + 1;
}
return [...mergedRows, item];
},
[rows[0]]
);
};
使用
3.1创建表
const columns: ProColumns<LabLocationType>[] = [
{
title: '学校名称',
dataIndex: 'school',
valueType: 'text',
ellipsis: true,
render: (text: any, record: any) => ({
children: <div style={{ fontWeight: "bold" }}>{text}</div>,
props: {
rowSpan: record.rowSpan,
colSpan: record.colSpan
}
})
},
]
3.2请求来的数据中对数据排序并且合并
request={(params) =>
getLabLocationList({
...params, parentId: 0
} as unknown as LabLocationListParams).then((res) => {
// getLabLocationTree();
return {
// data: res.rows,
data: mergeRows(classifyRows(res.rows, ['school']), 'school'),
total: res.rows.length,
success: true,
};
})
}
columns={columns}