From 8dc59fbec345267a904c72879351f8fd1ae5a653 Mon Sep 17 00:00:00 2001 From: wisonic-s <51065359+wisonic-s@users.noreply.github.com> Date: Wed, 16 Apr 2025 20:02:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=9B=BE=E8=A1=A8):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=80=8F=E8=A7=86=E8=A1=A8=E8=A1=8C=E5=88=97=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E4=B8=8D=E7=94=9F=E6=95=88=20#15798?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/panel/charts/table/table-pivot.ts | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-pivot.ts b/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-pivot.ts index b3eb3c63ca..2ebf9db2a8 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-pivot.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/charts/table/table-pivot.ts @@ -186,6 +186,7 @@ export class TablePivot extends S2ChartView { // 解析合计、小计排序 const sortParams = [] + let rowTotalSort = false if ( tableTotal.row.totalSort && tableTotal.row.totalSort !== 'none' && @@ -204,7 +205,9 @@ export class TablePivot extends S2ChartView { } sortParams.push(sort) }) + rowTotalSort = true } + let colTotalSort = false if ( tableTotal.col.totalSort && tableTotal.col.totalSort !== 'none' && @@ -223,6 +226,7 @@ export class TablePivot extends S2ChartView { } sortParams.push(sort) }) + colTotalSort = true } //列维度为空,行排序按照指标列来排序,取第一个有排序设置的指标 if (!columnFields?.length) { @@ -283,6 +287,80 @@ export class TablePivot extends S2ChartView { }) // 空值处理 const newData = this.configEmptyDataStrategy(chart) + // 行列维度排序 + if (!rowTotalSort) { + c?.forEach((f, i) => { + if (valueFieldMap[f]?.sort === 'none') { + return + } + const sort = { + sortFieldId: f + } + const sortMethod = valueFieldMap[f]?.sort?.toUpperCase() + if (sortMethod === 'CUSTOM_SORT') { + sort.sortBy = valueFieldMap[f].customSort + } else { + if (i === 0) { + sort.sortMethod = sortMethod + } else { + const fieldValues = newData.map(item => item[f]) + const uniqueValues = [...new Set(fieldValues)] + + // 根据配置动态决定排序顺序 + uniqueValues.sort((a, b) => { + if (!a && !b) { + return 0 + } + if (!a) { + return sortMethod === 'ASC' ? -1 : 1 + } + if (!b) { + return sortMethod === 'ASC' ? 1 : -1 + } + return sortMethod === 'ASC' ? a.localeCompare(b) : b.localeCompare(a) + }) + sort.sortBy = uniqueValues + } + } + sortParams.push(sort) + }) + } + if (!colTotalSort) { + r?.forEach((f, i) => { + if (valueFieldMap[f]?.sort === 'none') { + return + } + const sort = { + sortFieldId: f + } + const sortMethod = valueFieldMap[f]?.sort?.toUpperCase() + if (sortMethod === 'CUSTOM_SORT') { + sort.sortBy = valueFieldMap[f].customSort + } else { + if (i === 0) { + sort.sortMethod = sortMethod + } else { + const fieldValues = newData.map(item => item[f]) + const uniqueValues = [...new Set(fieldValues)] + // 根据配置动态决定排序顺序 + uniqueValues.sort((a, b) => { + if (!a && !b) { + return 0 + } + if (!a) { + return sortMethod === 'ASC' ? -1 : 1 + } + if (!b) { + return sortMethod === 'ASC' ? 1 : -1 + } + return sortMethod === 'ASC' ? a.localeCompare(b) : b.localeCompare(a) + }) + sort.sortBy = uniqueValues + } + } + sortParams.push(sort) + }) + } // data config const s2DataConfig: S2DataConfig = { fields: {