Skip to content

Commit 844a67e

Browse files
committed
refactor(igx-grid): Remove rAF calls from dimension setters - master
1 parent 258aa9b commit 844a67e

File tree

8 files changed

+115
-83
lines changed

8 files changed

+115
-83
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ import { DisplayDensity } from '../core/displayDensity';
5555
template: ``
5656
})
5757
export class IgxColumnComponent implements AfterContentInit {
58+
private _filterable = true;
59+
private _groupable = false;
5860
/**
5961
* Sets/gets the `field` value.
6062
* ```typescript
@@ -105,7 +107,15 @@ export class IgxColumnComponent implements AfterContentInit {
105107
* @memberof IgxColumnComponent
106108
*/
107109
@Input()
108-
public groupable = false;
110+
public get groupable() {
111+
return this._groupable;
112+
}
113+
public set groupable(val) {
114+
this._groupable = val;
115+
if (this.grid) {
116+
this.grid.cdr.markForCheck();
117+
}
118+
}
109119
/**
110120
* Sets/gets whether the column is editable.
111121
* Default value is `false`.
@@ -131,7 +141,15 @@ export class IgxColumnComponent implements AfterContentInit {
131141
* @memberof IgxColumnComponent
132142
*/
133143
@Input()
134-
public filterable = true;
144+
public get filterable() {
145+
return this._filterable;
146+
}
147+
public set filterable(val) {
148+
this._filterable = val;
149+
if (this.grid) {
150+
this.grid.cdr.markForCheck();
151+
}
152+
}
135153
/**
136154
* Sets/gets whether the column is resizable.
137155
* Default value is `false`.
@@ -300,6 +318,8 @@ export class IgxColumnComponent implements AfterContentInit {
300318
this._width = value;
301319
if (this.grid) {
302320
this.cacheCalcWidth();
321+
(this.grid as any)._derivePossibleWidth();
322+
this.grid.cdr.detectChanges();
303323
}
304324
}
305325
}

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

Lines changed: 28 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import {
2323
ViewChildren,
2424
ViewContainerRef,
2525
InjectionToken,
26-
Optional
26+
Optional,
27+
OnChanges,
28+
SimpleChanges
2729
} from '@angular/core';
2830
import { Subject } from 'rxjs';
2931
import { takeUntil, first, filter } from 'rxjs/operators';
@@ -233,8 +235,10 @@ export enum GridKeydownTargetType {
233235
hierarchicalRow = 'hierarchicalRow'
234236
}
235237

236-
export abstract class IgxGridBaseComponent extends DisplayDensityBase implements OnInit, OnDestroy, AfterContentInit, AfterViewInit {
238+
export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
239+
OnInit, OnChanges, OnDestroy, AfterContentInit, AfterViewInit {
237240
private _scrollWidth: number;
241+
protected _init = true;
238242

239243
public get scrollWidth() {
240244
return this._scrollWidth;
@@ -634,12 +638,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
634638
if (this._height !== value) {
635639
this._height = value;
636640
this._autoSize = false;
637-
requestAnimationFrame(() => {
638-
if (!this._destroyed) {
639-
this.reflow();
640-
this.cdr.markForCheck();
641-
}
642-
});
641+
this.nativeElement.style.height = value;
643642
}
644643
}
645644

@@ -653,28 +652,13 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
653652
@WatchChanges()
654653
@HostBinding('style.width')
655654
@Input()
656-
public get width() {
655+
get width() {
657656
return this._width;
658657
}
659-
660-
/**
661-
* Sets the width of the `IgxGridComponent`.
662-
* ```html
663-
* <igx-grid #grid [data]="Data" [width]="'305px'" [autoGenerate]="true"></igx-grid>
664-
* ```
665-
* @memberof IgxGridBaseComponent
666-
*/
667-
public set width(value: string) {
658+
set width(value) {
668659
if (this._width !== value) {
669660
this._width = value;
670-
requestAnimationFrame(() => {
671-
// Calling reflow(), because the width calculation
672-
// might make the horizontal scrollbar appear/disappear.
673-
// This will change the height, which should be recalculated.
674-
if (!this._destroyed) {
675-
this.reflow();
676-
}
677-
});
661+
this.nativeElement.style.width = value;
678662
}
679663
}
680664

@@ -686,7 +670,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
686670
* @memberof IgxGridBaseComponent
687671
*/
688672
get headerWidth() {
689-
return parseInt(this._width, 10) - 17;
673+
return parseInt(this.width, 10) - 17;
690674
}
691675

692676
/**
@@ -1986,49 +1970,6 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
19861970
this.cdr.markForCheck();
19871971
}
19881972

1989-
/**
1990-
* Returns the state of the grid virtualization, including the start index and how many records are rendered.
1991-
* ```typescript
1992-
* const gridVirtState = this.grid1.virtualizationState;
1993-
* ```
1994-
* @memberof IgxGridBaseComponent
1995-
*/
1996-
get virtualizationState() {
1997-
return this.verticalScrollContainer.state;
1998-
}
1999-
2000-
/**
2001-
* @hidden
2002-
*/
2003-
set virtualizationState(state) {
2004-
this.verticalScrollContainer.state = state;
2005-
}
2006-
2007-
/**
2008-
* Returns the total number of records in the data source.
2009-
* Works only with remote grid virtualization.
2010-
* ```typescript
2011-
* const itemCount = this.grid1.totalItemCount;
2012-
* ```
2013-
* @memberof IgxGridBaseComponent
2014-
*/
2015-
get totalItemCount() {
2016-
return this.verticalScrollContainer.totalItemCount;
2017-
}
2018-
2019-
/**
2020-
* Sets the total number of records in the data source.
2021-
* This property is required for virtualization to function when the grid is bound remotely.
2022-
* ```typescript
2023-
* this.grid1.totalItemCount = 55;
2024-
* ```
2025-
* @memberof IgxGridBaseComponent
2026-
*/
2027-
set totalItemCount(count) {
2028-
this.verticalScrollContainer.totalItemCount = count;
2029-
this.cdr.detectChanges();
2030-
}
2031-
20321973
/**
20331974
* @hidden
20341975
*/
@@ -2651,6 +2592,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
26512592
public summaryService: IgxGridSummaryService,
26522593
@Optional() @Inject(DisplayDensityToken) protected _displayDensityOptions: IDisplayDensityOptions) {
26532594
super(_displayDensityOptions);
2595+
this.cdr.detach();
26542596
this.resizeHandler = () => {
26552597
this.zone.run(() => this.calculateGridSizes());
26562598
};
@@ -2664,6 +2606,14 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
26642606
this.summaryService.grid = this;
26652607
}
26662608

2609+
ngOnChanges(changes: SimpleChanges) {
2610+
if (this._init) { return; }
2611+
const { height, width } = changes;
2612+
if (height || width) {
2613+
this.calculateGridSizes();
2614+
}
2615+
}
2616+
26672617
_setupListeners() {
26682618
const destructor = takeUntil(this.destroy$);
26692619

@@ -2715,7 +2665,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
27152665
this._setupServices();
27162666
this._setupListeners();
27172667
this.columnListDiffer = this.differs.find([]).create(null);
2718-
this.calcWidth = this._width && this._width.indexOf('%') === -1 ? parseInt(this._width, 10) : 0;
2668+
this.calcWidth = this.width && this.width.indexOf('%') === -1 ? parseInt(this.width, 10) : 0;
27192669
this.shouldGenerate = this.autoGenerate;
27202670
this._scrollWidth = this.getScrollWidth();
27212671
}
@@ -2870,6 +2820,8 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
28702820
}
28712821
});
28722822
});
2823+
this._init = false;
2824+
this.cdr.reattach();
28732825
}
28742826

28752827
private combineForOfCollections(dataList, summaryList) {
@@ -4008,7 +3960,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
40083960
* @hidden
40093961
*/
40103962
protected get isPercentWidth() {
4011-
return this._width && this._width.indexOf('%') !== -1;
3963+
return this.width && this.width.indexOf('%') !== -1;
40123964
}
40133965

40143966
/**
@@ -4031,6 +3983,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
40313983
column.defaultWidth = columnWidthCombined + 'px';
40323984
} else {
40333985
column.defaultWidth = this._columnWidth;
3986+
column.resetCaches();
40343987
}
40353988
});
40363989
this.resetCachedWidths();
@@ -4041,7 +3994,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
40413994
* @hidden
40423995
*/
40433996
protected get defaultTargetBodyHeight(): number {
4044-
const allItems = this.totalItemCount || this.dataLength;
3997+
const allItems = this.dataLength;
40453998
return this.renderedRowHeight * Math.min(this._defaultTargetRecordNumber,
40463999
this.paging ? Math.min(allItems, this.perPage) : allItems);
40474000
}
@@ -4232,7 +4185,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
42324185
width = computed.getPropertyValue('width').indexOf('%') === -1 ?
42334186
parseInt(computed.getPropertyValue('width'), 10) : null;
42344187
} else {
4235-
width = parseInt(this._width, 10);
4188+
width = parseInt(this.width, 10);
42364189
}
42374190

42384191
if (!width && el) {
@@ -4391,7 +4344,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
43914344
protected getUnpinnedWidth(takeHidden = false) {
43924345
let width = this.isPercentWidth ?
43934346
this.calcWidth :
4394-
parseInt(this._width, 10);
4347+
parseInt(this.width, 10);
43954348
if (this.hasVerticalSroll() && !this.isPercentWidth) {
43964349
width -= this.scrollWidth;
43974350
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,6 +1899,7 @@ describe('IgxGrid Component Tests', () => {
18991899

19001900
cell.inEditMode = true;
19011901
cell.update('IG');
1902+
fix.detectChanges();
19021903
cell.inEditMode = false;
19031904

19041905
await wait(DEBOUNCETIME);

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class IgxGridComponent extends IgxGridBaseComponent implements IGridDataB
117117
* ```html
118118
* <igx-grid [data]="Data" [autoGenerate]="true"></igx-grid>
119119
* ```
120-
* @memberof IgxGridBaseComponent
120+
* @memberof IgxGridComponent
121121
*/
122122
@Input()
123123
public get data(): any[] {
@@ -163,6 +163,49 @@ export class IgxGridComponent extends IgxGridBaseComponent implements IGridDataB
163163
}
164164
}
165165

166+
/**
167+
* Returns the state of the grid virtualization, including the start index and how many records are rendered.
168+
* ```typescript
169+
* const gridVirtState = this.grid1.virtualizationState;
170+
* ```
171+
* @memberof IgxGridComponent
172+
*/
173+
get virtualizationState() {
174+
return this.verticalScrollContainer.state;
175+
}
176+
177+
/**
178+
* @hidden
179+
*/
180+
set virtualizationState(state) {
181+
this.verticalScrollContainer.state = state;
182+
}
183+
184+
/**
185+
* Sets the total number of records in the data source.
186+
* This property is required for remote grid virtualization to function when it is bound to remote data.
187+
* ```typescript
188+
* this.grid1.totalItemCount = 55;
189+
* ```
190+
* @memberof IgxGridComponent
191+
*/
192+
set totalItemCount(count) {
193+
this.verticalScrollContainer.totalItemCount = count;
194+
this.cdr.detectChanges();
195+
}
196+
197+
/**
198+
* Returns the total number of records in the data source.
199+
* Works only with remote grid virtualization.
200+
* ```typescript
201+
* const itemCount = this.grid1.totalItemCount;
202+
* ```
203+
* @memberof IgxGridComponent
204+
*/
205+
get totalItemCount() {
206+
return this.verticalScrollContainer.totalItemCount;
207+
}
208+
166209
private _gridAPI: IgxGridAPIService;
167210
private _filteredData = null;
168211

@@ -351,6 +394,7 @@ export class IgxGridComponent extends IgxGridBaseComponent implements IGridDataB
351394
@Input()
352395
set dropAreaMessage(value: string) {
353396
this._dropAreaMessage = value;
397+
this.cdr.markForCheck();
354398
}
355399

356400
/**
@@ -780,6 +824,15 @@ export class IgxGridComponent extends IgxGridBaseComponent implements IGridDataB
780824
}
781825
}
782826

827+
/**
828+
* @hidden
829+
*/
830+
protected get defaultTargetBodyHeight(): number {
831+
const allItems = this.totalItemCount || this.dataLength;
832+
return this.renderedRowHeight * Math.min(this._defaultTargetRecordNumber,
833+
this.paging ? Math.min(allItems, this.perPage) : allItems);
834+
}
835+
783836
/**
784837
* @hidden
785838
*/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,7 @@ describe('IgxGrid - GroupBy', () => {
22422242
const fix = TestBed.createComponent(DefaultGridComponent);
22432243
fix.detectChanges();
22442244

2245-
fix.componentInstance.instance.dropAreaTemplate = fix.componentInstance.dropAreaTemplate;
2245+
fix.componentInstance.currentDropArea = fix.componentInstance.dropAreaTemplate;
22462246
await wait();
22472247
fix.detectChanges();
22482248

@@ -2591,6 +2591,7 @@ describe('IgxGrid - GroupBy', () => {
25912591
<igx-grid
25922592
[width]='width'
25932593
[height]='height'
2594+
[dropAreaTemplate]='currentDropArea'
25942595
[data]="data"
25952596
[autoGenerate]="true" (onColumnInit)="columnsCreated($event)" (onGroupingDone)="onGroupingDoneHandler($event)">
25962597
</igx-grid>
@@ -2602,6 +2603,7 @@ describe('IgxGrid - GroupBy', () => {
26022603
export class DefaultGridComponent extends DataParent {
26032604
public width = '800px';
26042605
public height = null;
2606+
public currentDropArea;
26052607

26062608
@ViewChild(IgxGridComponent, { read: IgxGridComponent, static: true })
26072609
public instance: IgxGridComponent;

0 commit comments

Comments
 (0)