Skip to content

Commit 591c0c7

Browse files
committed
perf(resizer): autosizeColumns is called too many times on page load
1 parent ad2f585 commit 591c0c7

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/app/modules/angular-slickgrid/components/__tests__/angular-slickgrid.component.spec.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,20 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
564564
sharedService.slickGrid = mockGrid as unknown as SlickGrid;
565565
});
566566

567+
it('should expect "autosizeColumns" being called when "autoFitColumnsOnFirstLoad" is set we udpated the dataset', () => {
568+
const autosizeSpy = jest.spyOn(mockGrid, 'autosizeColumns');
569+
const refreshSpy = jest.spyOn(component, 'refreshGridData');
570+
const mockData = [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Jane', lastName: 'Smith' }];
571+
jest.spyOn(mockDataView, 'getLength').mockReturnValueOnce(0).mockReturnValueOnce(0).mockReturnValueOnce(mockData.length);
572+
573+
component.ngAfterViewInit();
574+
component.gridOptions = { autoFitColumnsOnFirstLoad: true };
575+
component.setData(mockData, true); // manually force an autoresize
576+
577+
expect(autosizeSpy).toHaveBeenCalledTimes(2); // 1x by datasetChanged and 1x by bindResizeHook
578+
expect(refreshSpy).toHaveBeenCalledWith(mockData);
579+
});
580+
567581
it('should expect "autosizeColumns" being called when "autoFitColumnsOnFirstLoad" is set and we are on first page load', () => {
568582
const autosizeSpy = jest.spyOn(mockGrid, 'autosizeColumns');
569583
const refreshSpy = jest.spyOn(component, 'refreshGridData');
@@ -574,7 +588,7 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
574588
component.gridOptions = { autoFitColumnsOnFirstLoad: true };
575589
component.dataset = mockData;
576590

577-
expect(autosizeSpy).toHaveBeenCalledTimes(3); // 1x by datasetChanged and 2x by bindResizeHook
591+
expect(autosizeSpy).toHaveBeenCalledTimes(1);
578592
expect(refreshSpy).toHaveBeenCalledWith(mockData);
579593
});
580594

src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
102102
protected _eventPubSubService!: EventPubSubService;
103103
protected _angularGridInstances: AngularGridInstance | undefined;
104104
protected _hideHeaderRowAfterPageLoad = false;
105+
protected _isAutosizeColsCalled = false;
105106
protected _isGridInitialized = false;
106107
protected _isDatasetInitialized = false;
107108
protected _isDatasetHierarchicalInitialized = false;
@@ -187,7 +188,7 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
187188

188189
@Input()
189190
get dataset(): any[] {
190-
return (this.customDataView ? this.slickGrid?.getData?.() : this.dataView?.getItems?.()) || [];
191+
return (this.customDataView ? this.slickGrid?.getData?.() : this.dataView?.getItems()) || [];
191192
}
192193
set dataset(newDataset: any[]) {
193194
const prevDatasetLn = this._currentDatasetLength;
@@ -205,8 +206,9 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
205206

206207
// expand/autofit columns on first page load
207208
// we can assume that if the prevDataset was empty then we are on first load
208-
if (this.gridOptions?.autoFitColumnsOnFirstLoad && prevDatasetLn === 0) {
209+
if (this.slickGrid && this.gridOptions?.autoFitColumnsOnFirstLoad && prevDatasetLn === 0 && !this._isAutosizeColsCalled) {
209210
this.slickGrid.autosizeColumns();
211+
this._isAutosizeColsCalled = true;
210212
}
211213
}
212214

@@ -468,6 +470,7 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
468470
initialization(eventHandler: SlickEventHandler) {
469471
this.gridOptions.translater = this.translaterService;
470472
this._eventHandler = eventHandler;
473+
this._isAutosizeColsCalled = false;
471474

472475
// when detecting a frozen grid, we'll automatically enable the mousewheel scroll handler so that we can scroll from both left/right frozen containers
473476
if (this.gridOptions && ((this.gridOptions.frozenRow !== undefined && this.gridOptions.frozenRow >= 0) || this.gridOptions.frozenColumn !== undefined && this.gridOptions.frozenColumn >= 0) && this.gridOptions.enableMouseWheelScrollHandler === undefined) {
@@ -755,6 +758,14 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
755758
}
756759
}
757760

761+
setData(data: TData[], shouldAutosizeColumns = false) {
762+
if (shouldAutosizeColumns) {
763+
this._isAutosizeColsCalled = false;
764+
this._currentDatasetLength = 0;
765+
}
766+
this.dataset = data || [];
767+
}
768+
758769
/**
759770
* Check if there's any Pagination Presets defined in the Grid Options,
760771
* if there are then load them in the paginationOptions object
@@ -1004,21 +1015,17 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
10041015
throw new Error(`[Angular-Slickgrid] You cannot enable both autosize/fit viewport & resize by content, you must choose which resize technique to use. You can enable these 2 options ("autoFitColumnsOnFirstLoad" and "enableAutoSizeColumns") OR these other 2 options ("autosizeColumnsByCellContentOnFirstLoad" and "enableAutoResizeColumnsByCellContent").`);
10051016
}
10061017

1007-
// expand/autofit columns on first page load
1008-
if (grid && options.autoFitColumnsOnFirstLoad && options.enableAutoSizeColumns) {
1009-
grid.autosizeColumns();
1010-
}
1011-
10121018
// auto-resize grid on browser resize
10131019
if (options.gridHeight || options.gridWidth) {
10141020
this.resizerService.resizeGrid(0, { height: options.gridHeight, width: options.gridWidth });
10151021
} else {
10161022
this.resizerService.resizeGrid();
10171023
}
1018-
if (options.enableAutoResize) {
1019-
if (grid && options.autoFitColumnsOnFirstLoad && options.enableAutoSizeColumns) {
1020-
grid.autosizeColumns();
1021-
}
1024+
1025+
// expand/autofit columns on first page load
1026+
if (grid && options?.enableAutoResize && options.autoFitColumnsOnFirstLoad && options.enableAutoSizeColumns && !this._isAutosizeColsCalled) {
1027+
grid.autosizeColumns();
1028+
this._isAutosizeColsCalled = true;
10221029
}
10231030
}
10241031

0 commit comments

Comments
 (0)