Skip to content

Commit e561522

Browse files
authored
Merge pull request #5830 from IgniteUI/mkirova/fix-5751-8.1.x
fix(igxGrid): If set data is null/undefined update views accordingly.
2 parents 907a90e + a5dbd5b commit e561522

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
@@ -444,7 +444,7 @@ export class IgxForOfDirective<T> implements OnInit, OnChanges, DoCheck, OnDestr
444444
if (changes) {
445445
// re-init cache.
446446
if (!this.igxForOf) {
447-
return;
447+
this.igxForOf = [];
448448
}
449449
this._updateSizeCache();
450450
this._zone.run(() => {
@@ -1488,7 +1488,7 @@ export class IgxGridForOfDirective<T> extends IgxForOfDirective<T> implements On
14881488
this.onDataChanging.emit(args);
14891489
// re-init cache.
14901490
if (!this.igxForOf) {
1491-
return;
1491+
this.igxForOf = [];
14921492
}
14931493
/* we need to reset the master dir if all rows are removed
14941494
(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
@@ -150,6 +150,21 @@ describe('IgxGrid Component Tests', () => {
150150
}
151151
});
152152

153+
it('should remove all rows if data becomes null/undefined.', async () => {
154+
const fix = TestBed.createComponent(IgxGridRemoteVirtualizationComponent);
155+
fix.detectChanges();
156+
const grid = fix.componentInstance.instance;
157+
expect(grid.rowList.length).toEqual(10);
158+
159+
fix.componentInstance.nullData();
160+
fix.detectChanges();
161+
162+
const noRecordsSpan = fix.debugElement.query(By.css('.igx-grid__tbody-message'));
163+
expect(grid.rowList.length).toEqual(0);
164+
expect(noRecordsSpan).toBeTruthy();
165+
expect(noRecordsSpan.nativeElement.innerText).toBe('Grid has no data.');
166+
});
167+
153168
it('height/width should be calculated depending on number of records', fakeAsync(() => {
154169
const fix = TestBed.createComponent(IgxGridTestComponent);
155170
fix.componentInstance.grid.height = null;
@@ -4724,6 +4739,10 @@ export class LocalService {
47244739
this.records = this._records.asObservable();
47254740
}
47264741

4742+
nullData() {
4743+
this._records.next(null);
4744+
}
4745+
47274746
public getData(data?: IForOfState, cb?: (any) => void): any {
47284747
const size = data.chunkSize === 0 ? 10 : data.chunkSize;
47294748
this.dataStore = this.generateData(data.startIndex, data.startIndex + size);
@@ -4761,6 +4780,10 @@ export class IgxGridRemoteVirtualizationComponent implements OnInit, AfterViewIn
47614780
this.data = this.localService.records;
47624781
}
47634782

4783+
nullData() {
4784+
this.localService.nullData();
4785+
}
4786+
47644787
public ngAfterViewInit() {
47654788
this.localService.getData(this.instance.virtualizationState, (count) => {
47664789
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)