Skip to content

Commit 4cd54e8

Browse files
committed
fix: updateOption with dataSource object occor error #3768
1 parent 77a03df commit 4cd54e8

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

docs/assets/api/en/methods.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66

77
Update table configuration items, which will be automatically redrawn after being called.
88

9+
updateConfig parameter description:
10+
11+
- keepData: When the new option does not pass in records or dataSource, whether to retain the original data. The default is false
12+
913
```ts
1014
/**
1115
*Update options currently only support full updates
1216
* @param options
1317
*/
14-
updateOption(options: BaseTableConstructorOptions) => void
18+
updateOption(options: BaseTableConstructorOptions,updateConfig?:{
19+
//When the new option does not pass in records or dataSource, whether to retain the original data. The default is false
20+
keepData?:boolean
21+
}) => void
1522
```
1623

1724
If you need to update a single configuration item, please refer to the other `update**` interfaces below

docs/assets/api/zh/methods.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44

55
## updateOption(Function)
66

7-
更新表格配置项,调用后会自动重绘
7+
更新表格配置项,调用后会自动重绘。
8+
9+
updateConfig 参数说明:
10+
11+
- keepData: 当新的option没有传入records或者dataSource时,是否保留原本的数据。默认为false
812

913
```ts
1014
/**
1115
* 更新options 目前只支持全量更新
1216
* @param options
1317
*/
14-
updateOption(options: BaseTableConstructorOptions) => void
18+
updateOption(options: BaseTableConstructorOptions,updateConfig?:{
19+
//当新的option没有传入records或者dataSource时,是否保留原本的数据
20+
keepData?:boolean
21+
}) => void
1522
```
1623

1724
如果需要更新单个配置项,请参考下面其他`update**`接口

packages/vtable/src/ListTable.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import { getGroupCheckboxState, setCellCheckboxState } from './state/checkbox/ch
4343
import type { IEmptyTipComponent } from './components/empty-tip/empty-tip';
4444
import { Factory } from './core/factory';
4545
import { getGroupByDataConfig } from './core/group-helper';
46-
import type { CachedDataSource } from './data';
46+
import { DataSource, type CachedDataSource } from './data';
4747
import {
4848
listTableAddRecord,
4949
listTableAddRecords,
@@ -475,7 +475,15 @@ export class ListTable extends BaseTable implements ListTableAPI {
475475
}
476476
return ifCan;
477477
}
478-
updateOption(options: ListTableConstructorOptions) {
478+
updateOption(
479+
options: ListTableConstructorOptions,
480+
updateConfig: {
481+
//当新的option没有传入records或者dataSource时,是否保留原本的数据
482+
keepData?: boolean;
483+
} = {
484+
keepData: false
485+
}
486+
) {
479487
const internalProps = this.internalProps;
480488
super.updateOption(options);
481489
internalProps.frozenColDragHeaderMode =
@@ -512,21 +520,29 @@ export class ListTable extends BaseTable implements ListTableAPI {
512520
// this.hasMedia = null; // 避免重复绑定
513521
// 清空目前数据
514522
if (internalProps.releaseList) {
515-
internalProps.releaseList.forEach(releaseObj => releaseObj?.release?.());
516-
internalProps.releaseList = null;
523+
for (let i = internalProps.releaseList.length - 1; i >= 0; i--) {
524+
const releaseObj = internalProps.releaseList[i];
525+
if (updateConfig.keepData && releaseObj instanceof DataSource) {
526+
releaseObj.updateColumns(this.internalProps.columns);
527+
} else {
528+
releaseObj?.release?.();
529+
internalProps.releaseList.splice(i, 1);
530+
}
531+
}
517532
}
518533
// // 恢复selection状态
519534
// internalProps.selection.range = range;
520535
// this._updateSize();
521536
// 传入新数据
522-
if (options.dataSource) {
537+
if (options.dataSource && this.dataSource !== options.dataSource) {
523538
// _setDataSource(this, options.dataSource);
524539
this.dataSource = options.dataSource;
525540
} else if (options.records) {
526541
this.setRecords(options.records as any, {
527542
sortState: options.sortState
528543
});
529544
} else {
545+
this.refreshRowColCount();
530546
this._resetFrozenColCount();
531547
// 生成单元格场景树
532548
this.scenegraph.createSceneGraph();

packages/vtable/src/core/tableHelper.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ function getHierarchyExpandLevel(table: ListTableAPI) {
8686

8787
export function _setDataSource(table: BaseTableAPI, dataSource: DataSource): void {
8888
_dealWithUpdateDataSource(table, () => {
89-
table.internalProps.dataSource && table.internalProps.dataSource.release?.();
89+
if (table.internalProps.dataSource) {
90+
table.internalProps.releaseList.forEach(releaseObj => {
91+
if (releaseObj instanceof DataSource) {
92+
releaseObj.release();
93+
table.internalProps.releaseList.splice(table.internalProps.releaseList.indexOf(releaseObj), 1);
94+
}
95+
});
96+
}
9097
if (dataSource) {
9198
if (dataSource instanceof DataSource) {
9299
table.internalProps.dataSource = dataSource;

packages/vtable/src/data/DataSource.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,9 @@ export class DataSource extends EventTarget implements DataSourceAPI {
12851285
release(): void {
12861286
super.release?.();
12871287
this.lastFilterRules = null;
1288+
this.clearSortedMap();
1289+
this.clearCurrentIndexedData();
1290+
this.currentPagerIndexedData.length = 0;
12881291
}
12891292
clearSortedMap() {
12901293
this.currentIndexedData && (this.currentIndexedData.length = 0);

0 commit comments

Comments
 (0)