Skip to content

Commit a2d05b9

Browse files
authored
Merge pull request #5829 from IgniteUI/mkirova/fix-5751
fix(igxGrid): If set data is null/undefined update views accordingly.
2 parents 49aaa10 + 8c1f4d1 commit a2d05b9

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

projects/igniteui-angular/src/lib/directives/for-of/for_of.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
411411
if (changes) {
412412
// re-init cache.
413413
if (!this.igxForOf) {
414-
return;
414+
this.igxForOf = [];
415415
}
416416
this._updateSizeCache();
417417
this._zone.run(() => {
@@ -1451,7 +1451,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
14511451
this.onDataChanging.emit(args);
14521452
// re-init cache.
14531453
if (!this.igxForOf) {
1454-
return;
1454+
this.igxForOf = [];
14551455
}
14561456
/* we need to reset the master dir if all rows are removed
14571457
(e.g. because of filtering); if all columns are hidden, rows are

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ describe('IgxGrid Component Tests', () => {
145145
}
146146
});
147147

148+
it('should remove all rows if data becomes null/undefined.', async () => {
149+
const fix = TestBed.createComponent(IgxGridRemoteVirtualizationComponent);
150+
fix.detectChanges();
151+
const grid = fix.componentInstance.instance;
152+
expect(grid.rowList.length).toEqual(10);
153+
154+
fix.componentInstance.nullData();
155+
fix.detectChanges();
156+
157+
const noRecordsSpan = fix.debugElement.query(By.css('.igx-grid__tbody-message'));
158+
expect(grid.rowList.length).toEqual(0);
159+
expect(noRecordsSpan).toBeTruthy();
160+
expect(noRecordsSpan.nativeElement.innerText).toBe('Grid has no data.');
161+
});
162+
148163
it('height/width should be calculated depending on number of records', fakeAsync(() => {
149164
const fix = TestBed.createComponent(IgxGridTestComponent);
150165
fix.componentInstance.grid.height = null;
@@ -4424,6 +4439,10 @@ export class LocalService {
44244439
this.records = this._records.asObservable();
44254440
}
44264441

4442+
nullData() {
4443+
this._records.next(null);
4444+
}
4445+
44274446
public getData(data?: IForOfState, cb?: (any) => void): any {
44284447
const size = data.chunkSize === 0 ? 10 : data.chunkSize;
44294448
this.dataStore = this.generateData(data.startIndex, data.startIndex + size);
@@ -4461,6 +4480,10 @@ export class IgxGridRemoteVirtualizationComponent implements OnInit, AfterViewIn
44614480
this.data = this.localService.records;
44624481
}
44634482

4483+
nullData() {
4484+
this.localService.nullData();
4485+
}
4486+
44644487
public ngAfterViewInit() {
44654488
this.localService.getData(this.instance.virtualizationState, (count) => {
44664489
this.instance.totalItemCount = count;

src/app/grid-remote-virtualization/grid-remote-virtualization.sample.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
</igx-grid>
44

55
<button (click)="loadData()">loadData</button>
6+
<button (click)="loadNullData()">null Data</button>
7+
<button (click)="loadUndefinedData()">undefined Data</button>
68
</div>

src/app/grid-remote-virtualization/grid-remote-virtualization.sample.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,24 @@ export class GridVirtualizationSampleComponent implements OnInit, AfterViewInit
4646

4747
public loadData() {
4848
this.grid.shouldGenerate = true;
49+
this.remoteService.getData(this.grid.virtualizationState, (data) => {
50+
this.remoteData = this.remoteService.remoteData;
51+
});
52+
}
53+
54+
public loadNullData() {
55+
this.remoteService.nullData();
56+
this.remoteData = this.remoteService.remoteData;
57+
}
58+
59+
public loadUndefinedData() {
60+
this.remoteService.undefinedData();
4961
this.remoteData = this.remoteService.remoteData;
5062
}
5163

5264
public ngAfterViewInit() {
53-
this.remoteService.getData(this.grid.virtualizationState, (data) => {
54-
this.grid.totalItemCount = data['@odata.count'];
55-
});
65+
this.remoteService.nullData();
66+
this.remoteData = this.remoteService.remoteData;
5667
}
5768

5869
dataLoading(evt) {

src/app/shared/remote.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ export class RemoteService {
1616
this.remoteData = this._remoteData.asObservable();
1717
}
1818

19+
nullData() {
20+
this._remoteData.next(null);
21+
}
22+
23+
undefinedData() {
24+
this._remoteData.next(undefined);
25+
}
26+
1927
getData(data?: any, cb?: (any) => void) {
2028
const dataState = data;
2129
return this.http.get(this.buildUrl(dataState)).pipe(

0 commit comments

Comments
 (0)