Skip to content

Commit cae3118

Browse files
MayaKirovampavlinov
authored andcommitted
fix(igxGrid): Grid should render all columns when grid width is set to null. (#5395)
* fix(igxGrid): Grid should render all columns when grid width is set to null. * chore(*): Fixing build. * chore(*): Handling null width inetgration with other grid features that change the total grid width (row selectors, expansion indicators etc.). * chore(*): Additional handling for null width in combination with: hidden columns, multi-column headers, auto-generated columns, columns with no width. Making sure host binded width prop is set only after zone is stable to prevent timing issues where the same prop value changes during same change detection cycle and throws errors. * chore(*): Use zone.run for interactions that change the width when ran outside the zone (like resizing). * chore(*): If width is in % when grid width is null use min width. * chore(*): In case width is null allow setting null for width host bindings so that grid will expact based on content. In case width is null and column width in % then set width to min width explicitly. * chore(*): Fixing issues with width = null. * chore(*): In case width is null, always apply minwidth to columns as default. * chore(*): Make check more specific to null. * chore(*): Fixing merge issue. * chore(*): Fixing another test. * chore(*): DetectChanges before feature's column width is calculated so that all option changes are applied.
1 parent 2e62f6e commit cae3118

File tree

7 files changed

+114
-12
lines changed

7 files changed

+114
-12
lines changed

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

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
229229
private _observer: MutationObserver;
230230
protected _destroyed = false;
231231
private overlayIDs = [];
232+
private _hostWidth;
232233
/**
233234
* An accessor that sets the resource strings.
234235
* By default it uses EN resources.
@@ -595,6 +596,13 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
595596
}
596597
}
597598

599+
/**
600+
* @hidden
601+
*/
602+
@HostBinding('style.width')
603+
get hostWidth() {
604+
return this._width || this._hostWidth;
605+
}
598606
/**
599607
* Returns the width of the `IgxGridComponent`.
600608
* ```typescript
@@ -603,7 +611,6 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
603611
* @memberof IgxGridBaseComponent
604612
*/
605613
@WatchChanges()
606-
@HostBinding('style.width')
607614
@Input()
608615
public get width() {
609616
return this._width;
@@ -2921,6 +2928,11 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
29212928
return this._unpinnedWidth;
29222929
}
29232930

2931+
get isHorizontalScrollHidden() {
2932+
const diff = this.unpinnedWidth - this.totalWidth;
2933+
return this.width === null || diff >= 0;
2934+
}
2935+
29242936
/**
29252937
* @hidden
29262938
*/
@@ -3849,7 +3861,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
38493861
*/
38503862
protected _derivePossibleWidth() {
38513863
if (!this._columnWidthSetByUser) {
3852-
this._columnWidth = this.getPossibleColumnWidth();
3864+
this._columnWidth = this.width !== null ? this.getPossibleColumnWidth() : MINIMUM_COLUMN_WIDTH + 'px';
38533865
this.columnList.forEach((column: IgxColumnComponent) => {
38543866
column.defaultWidth = this._columnWidth;
38553867
});
@@ -4042,20 +4054,39 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
40424054
}
40434055

40444056

4045-
if (!width) {
4046-
width = this.columnList.reduce((sum, item) => sum + parseInt((item.width || item.defaultWidth), 10), 0);
4057+
if (this.width === null || !width) {
4058+
width = this.getColumnWidthSum();
40474059
}
40484060

4049-
if (this.hasVerticalSroll()) {
4061+
if (this.hasVerticalSroll() && this.width !== null) {
40504062
width -= this.scrollWidth;
40514063
}
4052-
if (Number.isFinite(width) && width !== this.calcWidth) {
4064+
if ((Number.isFinite(width) || width === null) && width !== this.calcWidth) {
40534065
this.calcWidth = width;
40544066
this.cdr.detectChanges();
40554067
}
40564068
this._derivePossibleWidth();
40574069
}
40584070

4071+
private getColumnWidthSum(): number {
4072+
let colSum = 0;
4073+
const cols = this.visibleColumns
4074+
.filter(x => !x.columnGroup);
4075+
cols.forEach((item) => {
4076+
const isWidthInPercent = item.width && typeof item.width === 'string' && item.width.indexOf('%') !== -1;
4077+
if (isWidthInPercent) {
4078+
item.width = MINIMUM_COLUMN_WIDTH + 'px';
4079+
}
4080+
colSum += parseInt((item.width || item.defaultWidth), 10) || MINIMUM_COLUMN_WIDTH;
4081+
});
4082+
if (!colSum) {
4083+
return null;
4084+
}
4085+
this.cdr.detectChanges();
4086+
colSum += this.getFeatureColumnsWidth();
4087+
return colSum;
4088+
}
4089+
40594090
public hasVerticalSroll() {
40604091
if (!this._ngAfterViewInitPassed) { return false; }
40614092
const isScrollable = this.verticalScrollContainer.isScrollable();
@@ -4148,6 +4179,48 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
41484179
this.cdr.detectChanges();
41494180
this.resetCaches();
41504181
}
4182+
4183+
if (this.zone.isStable) {
4184+
this.zone.run(() => {
4185+
this._applyWidthHostBinding();
4186+
this.cdr.detectChanges();
4187+
});
4188+
} else {
4189+
this.zone.onStable.pipe(first()).subscribe(() => {
4190+
this.zone.run(() => {
4191+
this._applyWidthHostBinding();
4192+
});
4193+
});
4194+
}
4195+
}
4196+
4197+
private _applyWidthHostBinding() {
4198+
let width = this._width;
4199+
if (width === null) {
4200+
let currentWidth = this.calcWidth;
4201+
if (this.hasVerticalSroll()) {
4202+
currentWidth += this.scrollWidth;
4203+
}
4204+
width = currentWidth + 'px';
4205+
this.resetCaches();
4206+
}
4207+
this._hostWidth = width;
4208+
this.cdr.markForCheck();
4209+
}
4210+
4211+
/**
4212+
* @hidden
4213+
* Gets the combined width of the columns that are specific to the enabled grid features. They are fixed.
4214+
* Method used to override the calculations.
4215+
*/
4216+
public getFeatureColumnsWidth() {
4217+
let width = 0;
4218+
4219+
if (this.headerCheckboxContainer) {
4220+
width += this.headerCheckboxContainer.nativeElement.getBoundingClientRect().width;
4221+
}
4222+
4223+
return width;
41514224
}
41524225

41534226
/**
@@ -4182,7 +4255,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
41824255
protected getUnpinnedWidth(takeHidden = false) {
41834256
let width = this.isPercentWidth ?
41844257
this.calcWidth :
4185-
parseInt(this._width, 10);
4258+
parseInt(this.width, 10) || parseInt(this.hostWidth, 10) || this.calcWidth;
41864259
if (this.hasVerticalSroll() && !this.isPercentWidth) {
41874260
width -= this.scrollWidth;
41884261
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3565,7 +3565,7 @@ describe('IgxGrid - Filtering actions - Excel style filtering', () => {
35653565
fix.detectChanges();
35663566

35673567
const headers: DebugElement[] = fix.debugElement.queryAll(By.directive(IgxGridHeaderGroupComponent));
3568-
const headerResArea = headers[2].children[0].nativeElement;
3568+
const headerResArea = headers[2].children[1].nativeElement;
35693569

35703570
const filterIcon = headerResArea.querySelector('.igx-excel-filter__icon');
35713571
filterIcon.click();

projects/igniteui-angular/src/lib/grids/grid/grid.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
</div>
7878

7979
<div igxGridBody class="igx-grid__tbody">
80-
<div class="igx-grid__tbody-content" role="rowgroup" (onDragStop)="selectionService.dragMode = $event" (onDragScroll)="dragScroll($event)" [igxGridDragSelect]="selectionService.dragMode" [style.height.px]='calcHeight' [style.width.px]='calcWidth + 1' #tbody (scroll)='scrollHandler($event)' (wheel)="wheelHandler()">
80+
<div class="igx-grid__tbody-content" role="rowgroup" (onDragStop)="selectionService.dragMode = $event" (onDragScroll)="dragScroll($event)" [igxGridDragSelect]="selectionService.dragMode" [style.height.px]='calcHeight' [style.width.px]='calcWidth ? calcWidth + 1 : null' #tbody (scroll)='scrollHandler($event)' (wheel)="wheelHandler()">
8181
<span *ngIf="hasMovableColumns && draggedColumn && pinnedColumns.length <= 0" [igxColumnMovingDrop]="parentVirtDir" [attr.droppable]="true" id="left" class="igx-grid__scroll-on-drag-left"></span>
8282
<span *ngIf="hasMovableColumns && draggedColumn && pinnedColumns.length > 0" [igxColumnMovingDrop]="parentVirtDir" [attr.droppable]="true" id="left" class="igx-grid__scroll-on-drag-pinned" [style.left.px]="pinnedWidth"></span>
8383
<ng-template igxGridFor let-rowData [igxGridForOf]="data
@@ -123,7 +123,7 @@
123123
<div class="igx-grid__tfoot-thumb" [hidden]='!hasVerticalSroll()' [style.height.px]='summariesHeight' [style.width.px]="scrollWidth"></div>
124124
</div>
125125

126-
<div class="igx-grid__scroll" [style.height]="'18px'" #scr [hidden]="unpinnedWidth - totalWidth >= 0">
126+
<div class="igx-grid__scroll" [style.height]="'18px'" #scr [hidden]="isHorizontalScrollHidden">
127127
<div class="igx-grid__scroll-start" [style.width.px]='pinnedWidth' [hidden]="pinnedWidth === 0"></div>
128128
<div class="igx-grid__scroll-main" [style.width.px]='unpinnedWidth'>
129129
<ng-template igxGridFor [igxGridForOf]='[]' #scrollContainer>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,22 @@ describe('IgxGrid Component Tests', () => {
13541354
expect(virtDir.getSizeAt(2)).toEqual(150);
13551355

13561356
});
1357+
1358+
it('should render all columns if grid width is set to null.', async () => {
1359+
const fix = TestBed.createComponent(IgxGridDefaultRenderingComponent);
1360+
fix.componentInstance.initColumnsRows(5, 30);
1361+
const grid = fix.componentInstance.grid;
1362+
fix.detectChanges();
1363+
1364+
grid.width = null;
1365+
fix.detectChanges();
1366+
await wait(16);
1367+
1368+
// grid should render all columns and all should be visible.
1369+
const cells = grid.getRowByIndex(0).cells;
1370+
expect(cells.length).toBe(30);
1371+
expect(parseInt(grid.hostWidth, 10)).toBe(30 * 136);
1372+
});
13571373
});
13581374

13591375
describe('IgxGrid - API methods', () => {

projects/igniteui-angular/src/lib/grids/hierarchical-grid/hierarchical-grid.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
<div class="igx-grid__tfoot-thumb" [hidden]='!hasVerticalSroll()' [style.height.px]='summariesHeight' [style.width.px]="scrollWidth"></div>
116116
</div>
117117

118-
<div class="igx-grid__scroll" [style.height]="'18px'" #scr [hidden]="unpinnedWidth - totalWidth >= 0">
118+
<div class="igx-grid__scroll" [style.height]="'18px'" #scr [hidden]="isHorizontalScrollHidden">
119119
<div class="igx-grid__scroll-start" [style.width.px]='pinnedWidth' [hidden]="pinnedWidth === 0"></div>
120120
<div class="igx-grid__scroll-main" [style.width.px]='unpinnedWidth'>
121121
<ng-template igxGridFor [igxGridForOf]='[]' #scrollContainer>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,19 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseCompone
518518
return width;
519519
}
520520

521+
/**
522+
* @hidden
523+
* Gets the combined width of the columns that are specific to the enabled grid features. They are fixed.
524+
* Method used to override the calculations.
525+
*/
526+
public getFeatureColumnsWidth() {
527+
let width = super.getFeatureColumnsWidth();
528+
if (this.hasExpandableChildren) {
529+
width += this.headerHierarchyExpander.nativeElement.clientWidth || this.getDefaultExpanderWidth();
530+
}
531+
return width;
532+
}
533+
521534
private getDefaultExpanderWidth(): number {
522535
switch (this.displayDensity) {
523536
case DisplayDensity.cosy:

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<div class="igx-grid__tfoot-thumb" [hidden]='!hasVerticalSroll()' [style.height.px]='summariesHeight' [style.width.px]="scrollWidth"></div>
9292
</div>
9393

94-
<div class="igx-grid__scroll" [style.height]="'18px'" #scr [hidden]="unpinnedWidth - totalWidth >= 0">
94+
<div class="igx-grid__scroll" [style.height]="'18px'" #scr [hidden]="isHorizontalScrollHidden">
9595
<div class="igx-grid__scroll-start" [style.width.px]='pinnedWidth' [hidden]="pinnedWidth === 0"></div>
9696
<div class="igx-grid__scroll-main" [style.width.px]='unpinnedWidth'>
9797
<ng-template igxGridFor [igxGridForOf]='[]' #scrollContainer>

0 commit comments

Comments
 (0)