Skip to content

Commit c974006

Browse files
authored
fix(igxGrid): Fix when grid and cols have % width in hidden container. (#15160)
1 parent 8a219ec commit c974006

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

projects/igniteui-angular/src/lib/grids/columns/column.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2527,7 +2527,9 @@ export class IgxColumnComponent implements AfterContentInit, OnDestroy, ColumnTy
25272527
const colWidth = this.width;
25282528
const isPercentageWidth = colWidth && typeof colWidth === 'string' && colWidth.indexOf('%') !== -1;
25292529
const isAutoWidth = colWidth && typeof colWidth === 'string' && colWidth === 'fit-content';
2530-
if (isPercentageWidth) {
2530+
if (isPercentageWidth && this.grid.isColumnWidthSum) {
2531+
this._calcWidth = this.grid.minColumnWidth;
2532+
} else if (isPercentageWidth ) {
25312533
this._calcWidth = Math.floor(parseFloat(colWidth) / 100 * this.grid.calcWidth);
25322534
} else if (!colWidth || isAutoWidth && !this.autoSize) {
25332535
// no width

projects/igniteui-angular/src/lib/grids/common/grid.interface.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,10 @@ export interface GridType extends IGridDataBindable {
718718
isLoading: boolean;
719719
/** @hidden @internal */
720720
gridSize: Size;
721-
721+
/** @hidden @internal */
722+
isColumnWidthSum: boolean;
723+
/** @hidden @internal */
724+
minColumnWidth: number;
722725
/** Strategy, used for cloning the provided data. The type has one method, that takes any type of data */
723726
dataCloneStrategy: IDataCloneStrategy;
724727

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3001,6 +3001,12 @@ export abstract class IgxGridBaseDirective implements GridType,
30013001
* @hidden @internal
30023002
*/
30033003
public filteringPipeTrigger = 0;
3004+
3005+
/**
3006+
* @hidden @internal
3007+
*/
3008+
public isColumnWidthSum = false;
3009+
30043010
/**
30053011
* @hidden @internal
30063012
*/
@@ -3201,7 +3207,7 @@ export abstract class IgxGridBaseDirective implements GridType,
32013207
private _columnSelectionMode: GridSelectionMode = GridSelectionMode.none;
32023208

32033209
private lastAddedRowIndex;
3204-
protected isColumnWidthSum = false;
3210+
32053211
private _currencyPositionLeft: boolean;
32063212

32073213
private rowEditPositioningStrategy = new RowEditPositionStrategy({
@@ -3234,7 +3240,7 @@ export abstract class IgxGridBaseDirective implements GridType,
32343240
/**
32353241
* @hidden @internal
32363242
*/
3237-
protected get minColumnWidth() {
3243+
public get minColumnWidth() {
32383244
return MINIMUM_COLUMN_WIDTH;
32393245
}
32403246

@@ -6488,8 +6494,10 @@ export abstract class IgxGridBaseDirective implements GridType,
64886494

64896495

64906496
if (this.width === null || !width) {
6491-
width = this.getColumnWidthSum();
64926497
this.isColumnWidthSum = true;
6498+
width = this.getColumnWidthSum();
6499+
} else {
6500+
this.isColumnWidthSum = false;
64936501
}
64946502

64956503
if (this.hasVerticalScroll() && this.width !== null) {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,21 @@ describe('IgxGrid Component Tests #grid', () => {
18701870
expect(parseInt(grid.hostWidth, 10)).toBe(30 * 136);
18711871
});
18721872

1873+
it('should render grid and columns with correct width when all are in % and inside a hidden container.', () => {
1874+
// in this case since the grid width is 0, the grid will use the sum of the columns
1875+
// those should resolve to 136px, as per the docs
1876+
const fix = TestBed.createComponent(IgxGridColumnHiddenPercentageWidthComponent);
1877+
const grid = fix.componentInstance.grid;
1878+
grid.width = '100%';
1879+
// 4 cols - 10% width
1880+
fix.componentInstance.initColumnsRows(5, 4);
1881+
fix.detectChanges();
1882+
1883+
expect(grid.calcWidth).toBe(136*4);
1884+
expect(grid.columns[0].calcWidth).toBe(136);
1885+
expect(grid.columns[1].calcWidth).toBe(136);
1886+
});
1887+
18731888
it('should retain column with in % after hiding/showing grid with 100% width', () => {
18741889
const fix = TestBed.createComponent(IgxGridColumnPercentageWidthComponent);
18751890
fix.componentInstance.initColumnsRows(5, 3);
@@ -3100,6 +3115,18 @@ export class IgxGridColumnPercentageWidthComponent extends IgxGridDefaultRenderi
31003115
}
31013116
}
31023117

3118+
@Component({
3119+
template: `<igx-grid #grid [hidden]="hidden" [data]="data" [autoGenerate]="false">
3120+
<igx-column *ngFor="let col of columns" [width]="'10%'" [field]="col.key" [header]="col.key" [dataType]="col.dataType">
3121+
</igx-column>
3122+
</igx-grid>`,
3123+
standalone: true,
3124+
imports: [IgxGridComponent, IgxColumnComponent, NgFor]
3125+
})
3126+
export class IgxGridColumnHiddenPercentageWidthComponent extends IgxGridDefaultRenderingComponent {
3127+
public hidden = true;
3128+
}
3129+
31033130
@Component({
31043131
template: `<div>
31053132
<igx-grid #grid [data]="data" height='300px' [style.--ig-size]="1" [autoGenerate]="true"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ export class IgxPivotGridComponent extends IgxGridBaseDirective implements OnIni
479479
/**
480480
* @hidden @internal
481481
*/
482-
protected override get minColumnWidth() {
482+
public override get minColumnWidth() {
483483
if (this.superCompactMode) {
484484
return MINIMUM_COLUMN_WIDTH_SUPER_COMPACT;
485485
} else {

0 commit comments

Comments
 (0)