Skip to content

Commit c24e75d

Browse files
authored
Merge pull request #3784 from VisActor/3768-bug-updateoption-dataSource-error
3768 bug updateoption data source error
2 parents e2dc759 + 5c39ce6 commit c24e75d

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed
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: updateOption with dataSource object occor error #3768\n\n",
5+
"type": "none",
6+
"packageName": "@visactor/vtable"
7+
}
8+
],
9+
"packageName": "@visactor/vtable",
10+
"email": "892739385@qq.com"
11+
}

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,
@@ -478,7 +478,15 @@ export class ListTable extends BaseTable implements ListTableAPI {
478478
}
479479
return ifCan;
480480
}
481-
updateOption(options: ListTableConstructorOptions) {
481+
updateOption(
482+
options: ListTableConstructorOptions,
483+
updateConfig: {
484+
//当新的option没有传入records或者dataSource时,是否保留原本的数据
485+
keepData?: boolean;
486+
} = {
487+
keepData: false
488+
}
489+
) {
482490
const internalProps = this.internalProps;
483491
super.updateOption(options);
484492
internalProps.frozenColDragHeaderMode =
@@ -515,21 +523,29 @@ export class ListTable extends BaseTable implements ListTableAPI {
515523
// this.hasMedia = null; // 避免重复绑定
516524
// 清空目前数据
517525
if (internalProps.releaseList) {
518-
internalProps.releaseList.forEach(releaseObj => releaseObj?.release?.());
519-
internalProps.releaseList = null;
526+
for (let i = internalProps.releaseList.length - 1; i >= 0; i--) {
527+
const releaseObj = internalProps.releaseList[i];
528+
if (updateConfig.keepData && releaseObj instanceof DataSource) {
529+
releaseObj.updateColumns(this.internalProps.columns);
530+
} else {
531+
releaseObj?.release?.();
532+
internalProps.releaseList.splice(i, 1);
533+
}
534+
}
520535
}
521536
// // 恢复selection状态
522537
// internalProps.selection.range = range;
523538
// this._updateSize();
524539
// 传入新数据
525-
if (options.dataSource) {
540+
if (options.dataSource && this.dataSource !== options.dataSource) {
526541
// _setDataSource(this, options.dataSource);
527542
this.dataSource = options.dataSource;
528543
} else if (options.records) {
529544
this.setRecords(options.records as any, {
530545
sortState: options.sortState
531546
});
532547
} else {
548+
this.refreshRowColCount();
533549
this._resetFrozenColCount();
534550
// 生成单元格场景树
535551
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)