Skip to content

Commit 332efaf

Browse files
authored
Merge pull request #4963 from VisActor/4953-bug-editCell-rowhierarchytype-grid-tree
4953 bug edit cell rowhierarchytype grid tree
2 parents 4b4e9c6 + eede7a0 commit 332efaf

4 files changed

Lines changed: 159 additions & 11 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "fix: change dimension value to recalculate totalTree #4953\n\n",
5+
"type": "none",
6+
"packageName": "@visactor/vtable"
7+
}
8+
],
9+
"packageName": "@visactor/vtable",
10+
"email": "892739385@qq.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "fix: when change dimenison value should update cached changedTree value\n\n",
5+
"type": "none",
6+
"packageName": "@visactor/vtable"
7+
}
8+
],
9+
"packageName": "@visactor/vtable",
10+
"email": "892739385@qq.com"
11+
}

packages/vtable/src/PivotTable.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,25 +2026,39 @@ export class PivotTable extends BaseTable implements PivotTableAPI {
20262026
newValue
20272027
);
20282028
} else {
2029+
let indicatorPosition: { position: 'col' | 'row'; index?: number };
20292030
const colKeys = cellDimensionPath.colHeaderPaths
20302031
?.filter((path: any) => {
20312032
return !path.virtual;
20322033
})
2033-
.map((colPath: any) => {
2034+
.map((colPath: any, index: number) => {
2035+
if (colPath.indicatorKey) {
2036+
indicatorPosition = {
2037+
position: 'col',
2038+
index
2039+
};
2040+
}
20342041
return colPath.indicatorKey ?? colPath.value;
20352042
});
20362043
const rowKeys = cellDimensionPath.rowHeaderPaths
20372044
?.filter((path: any) => {
20382045
return !path.virtual;
20392046
})
2040-
.map((rowPath: any) => {
2047+
.map((rowPath: any, index: number) => {
2048+
if (rowPath.indicatorKey) {
2049+
indicatorPosition = {
2050+
position: 'row',
2051+
index
2052+
};
2053+
}
20412054
return rowPath.indicatorKey ?? rowPath.value;
20422055
});
20432056
this.dataset.changeTreeNodeValue(
2044-
!this.internalProps.layoutMap.indicatorsAsCol ? rowKeys.slice(0, -1) : rowKeys,
2045-
this.internalProps.layoutMap.indicatorsAsCol ? colKeys.slice(0, -1) : colKeys,
2057+
rowKeys,
2058+
colKeys,
20462059
(this.internalProps.layoutMap as PivotHeaderLayoutMap).getIndicatorKey(col, row),
2047-
newValue
2060+
newValue,
2061+
indicatorPosition
20482062
);
20492063
}
20502064
} else if (this.flatDataToObjects) {

packages/vtable/src/dataset/dataset.ts

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,8 @@ export class Dataset {
11351135
* @param rowKey
11361136
* @param colKey
11371137
* @param indicator
1138+
* @param considerChangedValue
1139+
* @param indicatorPosition 指标在行或列中的位置 changeTreeNodeValue方法中也有该参数
11381140
* @returns
11391141
*/
11401142
getAggregator(
@@ -2139,35 +2141,100 @@ export class Dataset {
21392141
}
21402142
}
21412143
}
2142-
2144+
/**
2145+
*
2146+
* @param rowKey
2147+
* @param colKey
2148+
* @param indicator
2149+
* @param newValue
2150+
* @param indicatorPosition
2151+
*/
21432152
changeTreeNodeValue(
21442153
rowKey: string[] | string = [],
21452154
colKey: string[] | string = [],
21462155
indicator: string,
2147-
newValue: string | number
2156+
newValue: string | number,
2157+
indicatorPosition?: { position: 'col' | 'row'; index?: number }
21482158
) {
21492159
const indicatorIndex = this.indicatorKeys.indexOf(indicator);
2150-
2160+
//#region 获取行和列的维度key,考虑到tree模式被折叠的情况 rowKey colKey会小于rows和columns的长度
2161+
let rowDimensionKey: string | undefined;
2162+
if (indicatorPosition?.position === 'row') {
2163+
rowDimensionKey = rowKey.length >= 2 ? this.rows[rowKey.length - 2] : undefined;
2164+
} else {
2165+
const rowIndex = rowKey.length - 1;
2166+
rowDimensionKey = rowIndex >= 0 && rowIndex < this.rows.length ? this.rows[rowIndex] : undefined;
2167+
}
2168+
let colDimensionKey: string | undefined;
2169+
if (indicatorPosition?.position === 'col') {
2170+
colDimensionKey = colKey.length >= 2 ? this.columns[colKey.length - 2] : undefined;
2171+
} else {
2172+
const colIndex = colKey.length - 1;
2173+
colDimensionKey = colIndex >= 0 && colIndex < this.columns.length ? this.columns[colIndex] : undefined;
2174+
}
2175+
//#endregion
21512176
let flatRowKey;
21522177
let flatColKey;
21532178
if (typeof rowKey === 'string') {
21542179
flatRowKey = rowKey;
21552180
} else {
2156-
flatRowKey = rowKey.join(this.stringJoinChar);
2181+
//考虑 指标key有可能在数组中间位置或者前面的可能 将其删除再添加到尾部
2182+
if (!indicatorPosition || indicatorPosition.position === 'row') {
2183+
rowKey.map((key, i) => {
2184+
if (key === indicator && (!isValid(indicatorPosition?.index) || i === indicatorPosition.index)) {
2185+
rowKey.splice(i, 1);
2186+
}
2187+
});
2188+
}
2189+
if (rowKey.length < this.rows.length && this.rowHierarchyType === 'grid-tree') {
2190+
// 如果是平铺树结构 小计需要处理补充到rowKey中
2191+
if (rowKey[0] === this.rowGrandTotalLabel) {
2192+
} else if (
2193+
((this.totals?.row?.subTotalsDimensions &&
2194+
this.totals?.row?.subTotalsDimensions?.length >= 1 &&
2195+
this.totals.row.subTotalsDimensions.find((dimension: string) => dimension === rowDimensionKey)) ||
2196+
this.totals?.row?.showSubTotalsOnTreeNode) &&
2197+
rowKey[rowKey.length - 1] !== this.rowSubTotalLabel
2198+
) {
2199+
rowKey.push(this.rowSubTotalLabel);
2200+
}
2201+
}
2202+
// flatRowKey = rowKey.join(this.stringJoinChar);
2203+
flatRowKey = join(rowKey, this.stringJoinChar);
21572204
}
21582205

21592206
if (typeof colKey === 'string') {
21602207
flatColKey = colKey;
21612208
} else {
2162-
flatColKey = colKey.join(this.stringJoinChar);
2209+
//考虑 指标key有可能在数组中间位置或者前面的可能 将其删除再添加到尾部
2210+
if (!indicatorPosition || indicatorPosition.position === 'col') {
2211+
colKey.map((key, i) => {
2212+
if (key === indicator && (!isValid(indicatorPosition?.index) || i === indicatorPosition.index)) {
2213+
colKey.splice(i, 1);
2214+
}
2215+
});
2216+
}
2217+
if (colKey.length < this.columns.length && this.columnHierarchyType === 'grid-tree') {
2218+
if (colKey[0] === this.colGrandTotalLabel) {
2219+
} else if (
2220+
((this.totals?.column?.subTotalsDimensions &&
2221+
this.totals?.column?.subTotalsDimensions?.length >= 1 &&
2222+
this.totals.column.subTotalsDimensions.find((dimension: string) => dimension === colDimensionKey)) ||
2223+
this.totals?.column?.showSubTotalsOnTreeNode) &&
2224+
colKey[colKey.length - 1] !== this.colSubTotalLabel
2225+
) {
2226+
colKey.push(this.colSubTotalLabel);
2227+
}
2228+
}
2229+
// flatColKey = colKey.join(this.stringJoinChar);
2230+
flatColKey = join(colKey, this.stringJoinChar);
21632231
}
21642232
//在newValue能转为数字的情况下 转为数字
21652233
if (typeof newValue === 'string') {
21662234
if (!isNaN(parseFloat(newValue))) {
21672235
newValue = parseFloat(newValue);
21682236
}
21692237
}
2170-
21712238
if (this.changedTree[flatRowKey]?.[flatColKey]) {
21722239
this.changedTree[flatRowKey][flatColKey][indicatorIndex] = newValue;
21732240
} else if (this.changedTree[flatRowKey]) {
@@ -2458,10 +2525,55 @@ export class Dataset {
24582525
}
24592526
}
24602527

2528+
//#region 更新 changedTree 中包含旧维度值的键
2529+
const newChangedTree: Record<string, Record<string, any[]>> = {};
2530+
for (const flatRowKey in this.changedTree) {
2531+
const rowKeyParts = flatRowKey.split(this.stringJoinChar);
2532+
let rowKeyUpdated = false;
2533+
const newRowKeyParts = rowKeyParts.map(part => {
2534+
if (part === String(oldValue)) {
2535+
rowKeyUpdated = true;
2536+
return String(value);
2537+
}
2538+
return part;
2539+
});
2540+
const newFlatRowKey = rowKeyUpdated ? join(newRowKeyParts, this.stringJoinChar) : flatRowKey;
2541+
2542+
const colKeysMap = this.changedTree[flatRowKey];
2543+
if (!newChangedTree[newFlatRowKey]) {
2544+
newChangedTree[newFlatRowKey] = {};
2545+
}
2546+
2547+
for (const flatColKey in colKeysMap) {
2548+
const colKeyParts = flatColKey.split(this.stringJoinChar);
2549+
let colKeyUpdated = false;
2550+
const newColKeyParts = colKeyParts.map(part => {
2551+
if (part === String(oldValue)) {
2552+
colKeyUpdated = true;
2553+
return String(value);
2554+
}
2555+
return part;
2556+
});
2557+
const newFlatColKey = colKeyUpdated ? join(newColKeyParts, this.stringJoinChar) : flatColKey;
2558+
2559+
newChangedTree[newFlatRowKey][newFlatColKey] = colKeysMap[flatColKey];
2560+
}
2561+
}
2562+
this.changedTree = newChangedTree;
2563+
//#endregion
24612564
this.rowFlatKeys = {};
24622565
this.colFlatKeys = {};
24632566
this.tree = {};
24642567
this.processRecords();
2568+
this.processCollectedValuesWithSumBy();
2569+
this.processCollectedValuesWithSortBy();
2570+
this.totalStatistics();
2571+
if ((this.dataConfig as IPivotChartDataConfig)?.isPivotChart) {
2572+
// 处理PivotChart双轴图0值对齐
2573+
// this.dealWithZeroAlign();
2574+
// 记录PivotChart维度对应的数据
2575+
this.cacheDeminsionCollectedValues();
2576+
}
24652577
}
24662578
}
24672579
/** 主要是树形结构懒加载使用 */

0 commit comments

Comments
 (0)