From 26bf09e0dc25165467bbd844a2b8ca0179ebf229 Mon Sep 17 00:00:00 2001 From: Joyboo Date: Sun, 28 Apr 2024 11:07:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=95=B0=E6=8D=AE=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E5=A4=9A=E7=BA=A7=E8=A1=A8?= =?UTF-8?q?=E5=A4=B4=E3=80=81=E5=A4=9A=E7=A7=8DdataIndex=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/settings/ExportData.vue | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/components/Table/src/components/settings/ExportData.vue b/src/components/Table/src/components/settings/ExportData.vue index c28f141a..bc07328e 100644 --- a/src/components/Table/src/components/settings/ExportData.vue +++ b/src/components/Table/src/components/settings/ExportData.vue @@ -54,6 +54,7 @@ import { dateUtil } from '/@/utils/dateUtil'; import { usePermission } from '/@/hooks/web/usePermission'; import type { WorkSheet, CellObject } from 'xlsx'; + import { omit } from 'lodash-es'; const props = defineProps({ setting: { @@ -164,9 +165,30 @@ } } + // 将BasicColumn的无限级children拍平为二级结构 + function flattenColumn(data: BasicColumn[]): BasicColumn[] { + let flattened: BasicColumn[] = []; + + for (const item of data) { + // 先判断children + if (item.children && item.children.length > 0) { + const children = flattenColumn(item.children); + children.map((chitem) => { + chitem.title = `${item.title}-${chitem.title}`; + }); + + flattened = flattened.concat(children); + } else { + flattened.push(omit(item, 'children')); + } + } + return flattened; + } + // 导出当前页 function exportCurrentPage() { - const columns = getColumns(); + const columns = flattenColumn(getColumns()); + // 表格数据 const dataSource = table.getDataSource(); // 合计行数据 @@ -187,28 +209,31 @@ const data: any[] = []; // 计算单行数据 - const tableRowRender = (itemData: any, itemIndex: number) => { + const tableRowRender = (itemRecord: any, itemIndex: number) => { const row = {}; - columns.forEach(({ dataIndex, customRender }, index) => { + columns.forEach(({ dataIndex = '', customRender }, index) => { if (isString(dataIndex)) { - const col: string = isFunction(customRender) + // 支持a.b.c语法,并向上兼容未来的antdv数组格式的dataIndex + const didx: string[] = isString(dataIndex) ? dataIndex.split('.') : dataIndex; + const itemValue = didx.reduce((out, item) => out[item], itemRecord); + + const col = isFunction(customRender) ? customRender({ - text: itemData[dataIndex], - record: itemData, - index: itemData[ROW_KEY], + text: itemValue, + record: itemRecord, + index: itemRecord[ROW_KEY], /** * 此量为自定义字段, 用于在customRender中判断是否导出模式,以返回不同类型值 * 为什么要有此量? 因为customRender允许返回 string | VNode | Jsx, 而导出模式只能为string类型 */ exportMode: true, }) - : itemData[dataIndex] ?? ''; + : itemValue; row[dataIndex] = col; // 计算每一列的ascii const ascii = String.fromCharCode(65 + index); - const option: CellObject = { t: isTime(col) ? 'd' : isNumeric(col) ? 'n' : 's' }; workSheetOpts[`${ascii}${itemIndex}`] = option; }