Skip to content

Commit 90d368d

Browse files
committed
fix(autosize): column autosize for hidden empty grid
1 parent 864ff85 commit 90d368d

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7382,13 +7382,13 @@ export abstract class IgxGridBaseDirective extends DisplayDensityBase implements
73827382
}
73837383
const cells = this._dataRowList.map(x => x.cells.find(c => c.column === col));
73847384
cells.forEach((cell) => cellsContentWidths.push(cell?.nativeElement?.offsetWidth || 0));
7385+
const header = this.headerCellList.find(x => x.column === col);
7386+
cellsContentWidths.push(header.nativeElement.offsetWidth);
73857387
const max = Math.max(...cellsContentWidths);
73867388
if (max === 0) {
73877389
// cells not in DOM yet...
73887390
continue;
73897391
}
7390-
const header = this.headerCellList.find(x => x.column === col);
7391-
cellsContentWidths.push(header.nativeElement.offsetWidth);
73927392
let maxSize = Math.ceil(Math.max(...cellsContentWidths)) + 1;
73937393
if (col.maxWidth && maxSize > col.maxWidthPx) {
73947394
maxSize = col.maxWidthPx;

projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,23 @@ describe('IgxGrid Component Tests #grid', () => {
18651865

18661866
expect(grid.columnList.get(0).width).toBe('50%');
18671867
});
1868+
1869+
it('should correctly autosize column headers when the grid container has no data and is initially hidden and then shown', async () => {
1870+
const fix = TestBed.createComponent(IgxGridColumnHeaderAutoSizeComponent);
1871+
const grid = fix.componentInstance.grid;
1872+
1873+
//waiting for reqeustAnimationFrame to finish
1874+
await wait(17);
1875+
fix.detectChanges();
1876+
1877+
fix.componentInstance.gridContainerHidden = false;
1878+
await wait(17)
1879+
fix.detectChanges()
1880+
1881+
const calcWidth = parseInt(grid.columnList.first.calcWidth, 10)
1882+
1883+
expect(calcWidth).not.toBe(80);
1884+
});
18681885
});
18691886

18701887
describe('IgxGrid - API methods', () => {
@@ -2880,6 +2897,39 @@ export class IgxGridDefaultRenderingComponent {
28802897
}
28812898
}
28822899

2900+
@Component({
2901+
template: `
2902+
<div [hidden]="gridContainerHidden">
2903+
<igx-grid #grid>
2904+
<igx-column
2905+
field="field1"
2906+
header="Field 1 Header"
2907+
width="auto"
2908+
></igx-column>
2909+
<igx-column
2910+
field="field2"
2911+
header="Field 2 Header"
2912+
width="auto"
2913+
></igx-column>
2914+
<igx-column
2915+
field="field3"
2916+
header="Field 3 Header"
2917+
width="auto"
2918+
></igx-column>
2919+
</igx-grid>
2920+
</div>`,
2921+
standalone: true,
2922+
imports: [IgxGridComponent, IgxColumnComponent]
2923+
2924+
})
2925+
export class IgxGridColumnHeaderAutoSizeComponent {
2926+
@ViewChild('grid', { read: IgxGridComponent, static: true })
2927+
public grid: IgxGridComponent;
2928+
2929+
public gridContainerHidden = true;
2930+
2931+
}
2932+
28832933
@Component({
28842934
template: `<igx-grid #grid [data]="data" [width]="'500px'" (columnInit)="initColumns($event)">
28852935
<igx-column *ngFor="let col of columns" [field]="col.key" [header]="col.key" [dataType]="col.dataType">

0 commit comments

Comments
 (0)