Skip to content

Commit

Permalink
Merge pull request #1519 from ghiscoding/bugfix/resize-data-length
Browse files Browse the repository at this point in the history
fix: add `autoResize.autoHeight` to resize by dataset length
  • Loading branch information
ghiscoding authored Jan 25, 2025
2 parents e41fe93 + 683b716 commit 33d0f04
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/app/examples/grid-tree-data-hierarchical.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class GridTreeDataHierarchicalComponent implements OnInit {

this.gridOptions = {
autoResize: {
autoHeight: false,
container: '#demo-container',
rightPadding: 10,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ Object.defineProperty(paginationServiceStub, 'totalItems', {
});

const resizerServiceStub = {
isAutoHeightEnabled: true,
autoHeightRecalcRow: 100,
init: jest.fn(),
dispose: jest.fn(),
bindAutoResizeDataGrid: jest.fn(),
Expand Down Expand Up @@ -2272,6 +2274,32 @@ describe('Angular-Slickgrid Custom Component instantiated via Constructor', () =
expect(footerSpy).toHaveBeenCalledWith(expectation);
});

it('should call a grid resize when the DataView "onRowCountChanged" event is triggered with a low dataset length and autoResize.autoHeight is enabled', () => {
const mockData = [
{ firstName: 'John', lastName: 'Doe' },
{ firstName: 'Jane', lastName: 'Smith' },
];
const invalidateSpy = jest.spyOn(mockGrid, 'invalidate');
const expectation = {
startTime: expect.any(Date),
endTime: expect.any(Date),
itemCount: 2,
totalItemCount: 2,
};
jest.spyOn(mockDataView, 'getItemCount').mockReturnValue(mockData.length);
jest.spyOn(mockDataView, 'getFilteredItemCount').mockReturnValue(mockData.length);
jest.spyOn(mockDataView, 'getLength').mockReturnValue(mockData.length);
const resizerSpy = jest.spyOn(resizerServiceStub, 'resizeGrid');

component.gridOptions = { enableAutoResize: true, autoResize: { autoHeight: true } };
component.initialization(slickEventHandler);
mockDataView.onRowCountChanged.notify({ current: 2, previous: 0, dataView: mockDataView, itemCount: 0, callingOnRowsChanged: false });

expect(invalidateSpy).toHaveBeenCalled();
expect(component.metrics).toEqual(expectation);
expect(resizerSpy).toHaveBeenCalled();
});

it('should have custom footer with metrics when the DataView "onSetItemsCalled" event is triggered', () => {
const expectation = {
startTime: expect.toBeDate(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,11 @@ export class AngularSlickgridComponent<TData = any> implements AfterViewInit, On
if (this._isLocalGrid && this.gridOptions?.enableEmptyDataWarningMessage) {
this.displayEmptyDataWarning(currentPageRowItemCount === 0);
}

// when autoResize.autoHeight is enabled, we'll want to call a resize
if (this.gridOptions.enableAutoResize && this.resizerService.isAutoHeightEnabled && currentPageRowItemCount > 0) {
this.resizerService.resizeGrid();
}
}

protected initializePaginationService(paginationOptions: Pagination) {
Expand Down
4 changes: 3 additions & 1 deletion src/app/modules/angular-slickgrid/global-grid-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export const GlobalGridOptions: Partial<GridOption> = {
autoFitColumnsOnFirstLoad: true,
autoResize: {
applyResizeToContainer: true,
autoHeight: true,
autoHeightRecalcRow: 100,
calculateAvailableSizeBy: 'window',
bottomPadding: 20,
minHeight: 180,
minHeight: 250,
minWidth: 300,
rightPadding: 0,
},
Expand Down

0 comments on commit 33d0f04

Please sign in to comment.