Skip to content

Commit 6f72683

Browse files
Merge pull request #3500 from IgniteUI/bpachilova/grid-cascading-combos-fix-3499
Refactor Grid Cascading Combos sample to handle vritualized grid scenario
2 parents cad7973 + 22455c0 commit 6f72683

2 files changed

Lines changed: 36 additions & 31 deletions

File tree

src/app/grid/grid-cascading-combos/grid-cascading-combos.component.html

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<div class="grid__wrapper">
22
<div class="sample__header">
3-
<igx-grid [igxPreventDocumentScroll]="true" #grid1 [data]="data" [autoGenerate]="false"
4-
width="100%" height="500px" [primaryKey]="'ID'">
3+
<igx-grid [igxPreventDocumentScroll]="true" #grid1 [data]="data" [autoGenerate]="false" width="100%"
4+
height="500px" [primaryKey]="'ID'">
55
<igx-column field="ID" header="ID" [dataType]="'number'" width="50px">
66
</igx-column>
7-
<igx-column field="Country" header="Country" [dataType]="'string'" width="220px">
7+
<igx-column field="Country" header="Country" dataType="string" width="220px">
88
<ng-template igxCell let-cell="cell">
99
<igx-simple-combo [id]="'country-' + cell.row.data.ID" [data]="countriesData"
1010
(selectionChanging)="countryChanging($event, cell)" placeholder="Choose Country..."
11-
[displayKey]="'name'" width="100%">
11+
[ngModel]="cell.value === '' ? undefined : cell.value" [valueKey]="'name'" [displayKey]="'name'"
12+
width="100%">
1213
</igx-simple-combo>
1314
</ng-template>
1415
</igx-column>
@@ -17,22 +18,24 @@
1718
<div>
1819
<igx-simple-combo [id]="'region-' + cell.row.data.ID"
1920
(selectionChanging)="regionChanging($event, cell)" placeholder="Choose Region..."
20-
[displayKey]="'name'" [(ngModel)]="cell.value"
21-
[disabled]="cell.row.cells[1].value === ''">
21+
[ngModel]="cell.value === '' ? undefined : cell.value" [valueKey]="'name'"
22+
[displayKey]="'name'" [disabled]="cell.row.cells[1].value === ''">
2223
</igx-simple-combo>
23-
<igx-linear-bar [id]="'region-progress-' + cell.row.data.ID" [style.visibility]="'hidden'"
24+
<igx-linear-bar *ngIf="cell.row.data.loadingRegion" [id]="'region-progress-' + cell.row.data.ID"
2425
type="info" [indeterminate]="true">
2526
</igx-linear-bar>
2627
</div>
2728
</ng-template>
2829
</igx-column>
29-
<igx-column field="City" header="City" width="220px" [editable]="true">
30+
<igx-column field="City" header="City" width="220px" dataType="number">
3031
<ng-template igxCell let-cell="cell">
3132
<div>
3233
<igx-simple-combo [id]="'city-' + cell.row.data.ID" placeholder="Choose City..."
33-
[(ngModel)]="cell.value" [displayKey]="'name'" [disabled]="cell.row.cells[2].value === ''">
34+
(selectionChanging)="cityChanging($event, cell)"
35+
[ngModel]="!cell.value ? undefined : cell.value" [valueKey]="'id'" [displayKey]="'name'"
36+
[disabled]="cell.row.cells[2].value === ''">
3437
</igx-simple-combo>
35-
<igx-linear-bar [id]="'city-progress-' + cell.row.data.ID" [style.visibility]="'hidden'"
38+
<igx-linear-bar *ngIf="cell.row.data.loadingCity" [id]="'city-progress-' + cell.row.data.ID"
3639
type="info" [indeterminate]="true">
3740
</igx-linear-bar>
3841
</div>

src/app/grid/grid-cascading-combos/grid-cascading-combos.component.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit, QueryList, ViewChildren } from '@angular/core';
22
import { IgxSimpleComboComponent, ISimpleComboSelectionChangingEventArgs } from 'igniteui-angular';
3-
import { City, Country, getCitiesByCountry, getCountries, Region } from '../../data/cities15000-regions-countries';
3+
import { Country, getCitiesByCountry, getCountries } from '../../data/cities15000-regions-countries';
44
import { DATA } from '../../data/data';
55

66
@Component({
@@ -12,9 +12,9 @@ export class GridCascadingCombosComponent implements OnInit {
1212
@ViewChildren(IgxSimpleComboComponent)
1313
public combos: QueryList<IgxSimpleComboComponent>;
1414

15-
public selectedCountry: Country;
16-
public selectedRegion: Region;
17-
public selectedCity: City;
15+
public selectedCountryName: string;
16+
public selectedRegionName: string;
17+
public selectedCityId: number;
1818
public countriesData: Country[];
1919
private loadingTime = 0;
2020
public data;
@@ -30,62 +30,64 @@ export class GridCascadingCombosComponent implements OnInit {
3030

3131
public countryChanging(e: ISimpleComboSelectionChangingEventArgs, cell) {
3232
const ID = cell.row.data.ID;
33+
cell.row.data.loadingRegion = true;
3334
const nextRegionCombo = this.combos.filter(
3435
(combo) => combo.id === 'region-' + ID
3536
)[0];
3637
const nextCityCombo = this.combos.filter(
3738
(combo) => combo.id === 'city-' + ID
3839
)[0];
3940
this.clearOldData(cell, nextRegionCombo, nextCityCombo);
40-
this.selectedCountry = e.newValue as Country;
41+
this.selectedCountryName = e.newValue;
4142
cell.update(e.newValue ? e.newValue : '');
4243
if (e.newValue) {
43-
document.getElementById('region-progress-' + ID).style.visibility = 'visible';
4444
this.loadingTime = 2000;
4545
}
4646
setTimeout(() => {
47-
nextRegionCombo.data = getCitiesByCountry([this.selectedCountry?.name])
47+
nextRegionCombo.data = getCitiesByCountry([this.selectedCountryName])
4848
.map((c) => ({ name: c.region, country: c.country }))
4949
.filter((v, i, a) => a.findIndex((r) => r.name === v.name) === i);
50-
document.getElementById('region-progress-' + ID).style.visibility = 'hidden';
50+
cell.row.data.loadingRegion = false;
5151
}, this.loadingTime);
52-
this.selectedRegion = null;
53-
this.selectedCity = null;
52+
this.selectedRegionName = null;
53+
this.selectedCityId = null;
5454
this.loadingTime = 0;
5555
}
5656

5757
public regionChanging(e: ISimpleComboSelectionChangingEventArgs, cell) {
5858
const nextComboID = 'city-' + cell.row.data.ID;
59+
cell.row.data.loadingCity = true;
5960
const cityCombo = this.combos.filter(
6061
(combo) => combo.id === nextComboID
6162
)[0];
6263
this.clearOldData(cell, null, cityCombo);
6364

64-
this.selectedRegion = e.newValue as Region;
65+
this.selectedRegionName = e.newValue;
6566
cell.update(e.newValue ? e.newValue : '');
6667
if (e.newValue) {
67-
document.getElementById(
68-
'city-progress-' + cell.row.data.ID
69-
).style.visibility = 'visible';
7068
this.loadingTime = 2000;
7169
}
7270
setTimeout(() => {
73-
cityCombo.data = getCitiesByCountry([this.selectedCountry?.name]).filter(
74-
(c) => c.region === this.selectedRegion?.name
71+
cityCombo.data = getCitiesByCountry([this.selectedCountryName]).filter(
72+
(c) => c.region === this.selectedRegionName
7573
);
76-
document.getElementById(
77-
'city-progress-' + cell.row.data.ID
78-
).style.visibility = 'hidden';
74+
cell.row.data.loadingCity = false;
7975
}, this.loadingTime);
80-
this.selectedCity = null;
76+
this.selectedCityId = null;
8177
this.loadingTime = 0;
8278
}
79+
public cityChanging(e: ISimpleComboSelectionChangingEventArgs, cell) {
80+
cell.update(e.newValue);
81+
this.selectedCityId = e.newValue;
82+
}
8383

8484
private clearOldData(cell, RegionCombo, CityCombo) {
8585
const nextCellIndex = cell.column.visibleIndex + 1;
8686
cell.row.cells[nextCellIndex].update('');
8787

88-
if (CityCombo !== null) CityCombo.data = [];
88+
if (CityCombo !== null) {
89+
CityCombo.data = [];
90+
}
8991
if (RegionCombo !== null) {
9092
RegionCombo.data = [];
9193
cell.row.cells[nextCellIndex + 1].update('');

0 commit comments

Comments
 (0)