diff --git a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts index d39dcef528b..591297dd700 100644 --- a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts +++ b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts @@ -2140,7 +2140,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements this.navigation.grid = this; this.filteringService.gridId = this.id; this.columnListDiffer = this.differs.find([]).create(null); - this.calcWidth = this._width && this._width.indexOf('%') === -1 ? parseInt(this._width, 10) : 0; + this.calcWidth = !this.isPercentWidth ? parseInt(this._width, 10) : 0; this.calcHeight = 0; this.calcRowCheckboxWidth = 0; @@ -3408,12 +3408,26 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements return 0; } + /** + * @hidden + */ + protected get isPercentWidth() { + return this._width && this._width.indexOf('%') !== -1; + } + + /** + * @hidden + */ + protected get isPercentHeight() { + return this._height && this._height.indexOf('%') !== -1; + } + /** * @hidden * Sets this._height */ protected _derivePossibleHeight() { - if ((this._height && this._height.indexOf('%') === -1) || !this._height || !this.isAttachedToDom) { + if (!this.isPercentHeight || !this._height || !this.isAttachedToDom || this.rowBasedHeight === 0) { return; } if (!this.nativeElement.parentNode || !this.nativeElement.parentNode.clientHeight) { @@ -3523,7 +3537,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements return null; } - if (this._height && this._height.indexOf('%') !== -1) { + if (this.isPercentHeight) { /*height in %*/ gridHeight = parseInt(computed.getPropertyValue('height'), 10); } else { @@ -3536,7 +3550,8 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements this.scr.nativeElement.clientHeight); if (height === 0 || isNaN(gridHeight)) { - return this.defaultTargetBodyHeight; + const bodyHeight = this.defaultTargetBodyHeight; + return bodyHeight > 0 ? bodyHeight : null; } return height; @@ -3584,7 +3599,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements const computed = this.document.defaultView.getComputedStyle(this.nativeElement); const el = this.document.getElementById(this.nativeElement.id); - if (this._width && this._width.indexOf('%') !== -1) { + if (this.isPercentWidth) { /* width in %*/ width = computed.getPropertyValue('width').indexOf('%') === -1 ? parseInt(computed.getPropertyValue('width'), 10) : null; @@ -3671,7 +3686,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements * @memberof IgxGridBaseComponent */ protected getUnpinnedWidth(takeHidden = false) { - const width = this._width && this._width.indexOf('%') !== -1 ? + const width = this.isPercentWidth ? this.calcWidth : parseInt(this._width, 10); return width - this.getPinnedWidth(takeHidden); diff --git a/projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts b/projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts index 25c77952a0b..48bd4e3b2d2 100644 --- a/projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts +++ b/projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts @@ -49,7 +49,8 @@ describe('IgxGrid Component Tests', () => { declarations: [ IgxGridTestComponent, IgxGridMarkupDeclarationComponent, - IgxGridRemoteVirtualizationComponent + IgxGridRemoteVirtualizationComponent, + IgxGridEmptyMessage100PercentComponent ], imports: [ NoopAnimationsModule, IgxGridModule.forRoot()] @@ -271,12 +272,17 @@ describe('IgxGrid Component Tests', () => { const grid = fixture.componentInstance.grid; const gridBody = fixture.debugElement.query(By.css(TBODY_CLASS)); + const domGrid = fixture.debugElement.query(By.css('igx-grid')).nativeElement; + + // make sure default width/height are applied when there is no data + expect(domGrid.style.height).toBe('100%'); + expect(domGrid.style.width).toBe('100%'); // Check for loaded rows in grid's container fixture.componentInstance.generateData(30); fixture.detectChanges(); tick(1000); - expect(parseInt(window.getComputedStyle(gridBody.nativeElement).height, 10)).toBeGreaterThan(1000); + expect(parseInt(window.getComputedStyle(gridBody.nativeElement).height, 10)).toBe(548); // Check for empty filter grid message and body less than 100px const columns = fixture.componentInstance.grid.columns; @@ -284,13 +290,13 @@ describe('IgxGrid Component Tests', () => { fixture.detectChanges(); tick(100); expect(gridBody.nativeElement.textContent).toEqual(grid.emptyFilteredGridMessage); - expect(parseInt(window.getComputedStyle(gridBody.nativeElement).height, 10)).toBeLessThan(100); + expect(parseInt(window.getComputedStyle(gridBody.nativeElement).height, 10)).toBe(548); // Clear filter and check if grid's body height is restored based on all loaded rows grid.clearFilter(columns[0].field); fixture.detectChanges(); tick(100); - expect(parseInt(window.getComputedStyle(gridBody.nativeElement).height, 10)).toBeGreaterThan(1000); + expect(parseInt(window.getComputedStyle(gridBody.nativeElement).height, 10)).toBe(548); // Clearing grid's data and check for empty grid message fixture.componentInstance.clearData(); @@ -299,6 +305,22 @@ describe('IgxGrid Component Tests', () => { expect(gridBody.nativeElement.innerText).toMatch(grid.emptyGridMessage); })); + + it('should render empty message when grid height is 100%', fakeAsync(() => { + const fixture = TestBed.createComponent(IgxGridEmptyMessage100PercentComponent); + fixture.detectChanges(); + + const grid = fixture.componentInstance.grid; + const gridBody = fixture.debugElement.query(By.css(TBODY_CLASS)); + const domGrid = fixture.debugElement.query(By.css('igx-grid')).nativeElement; + + // make sure default width/height are applied when there is no data + expect(domGrid.style.height).toBe('100%'); + expect(domGrid.style.width).toBe('100%'); + + expect(parseInt(window.getComputedStyle(gridBody.nativeElement).height, 10)).toBeGreaterThan(0); + expect(gridBody.nativeElement.innerText).toMatch(grid.emptyGridMessage); + })); }); describe('IgxGrid - default rendering for rows and columns', () => { @@ -3489,6 +3511,21 @@ export class IgxGridMarkupDeclarationComponent extends IgxGridTestComponent { public instance: IgxGridComponent; } +@Component({ + template: `