|
| 1 | +import { Component, OnInit, QueryList, ViewChildren } from '@angular/core'; |
| 2 | +import { IgxSimpleComboComponent, ISimpleComboSelectionChangingEventArgs } from 'igniteui-angular'; |
| 3 | +import { City, Country, getCitiesByCountry, getCountries, Region } from '../../data/cities15000-regions-countries'; |
| 4 | +import { DATA } from '../../data/data'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'grid-cascading-combos', |
| 8 | + templateUrl: './grid-cascading-combos.component.html', |
| 9 | + styleUrls: ['./grid-cascading-combos.component.scss'] |
| 10 | +}) |
| 11 | +export class GridCascadingCombosComponent implements OnInit { |
| 12 | + @ViewChildren(IgxSimpleComboComponent) |
| 13 | + public combos: QueryList<IgxSimpleComboComponent>; |
| 14 | + |
| 15 | + public selectedCountry: Country; |
| 16 | + public selectedRegion: Region; |
| 17 | + public selectedCity: City; |
| 18 | + public countriesData: Country[]; |
| 19 | + private loadingTime = 0; |
| 20 | + public data; |
| 21 | + |
| 22 | + public ngOnInit() { |
| 23 | + this.data = DATA; |
| 24 | + this.countriesData = getCountries([ |
| 25 | + 'United States', |
| 26 | + 'Japan', |
| 27 | + 'United Kingdom' |
| 28 | + ]); |
| 29 | + } |
| 30 | + |
| 31 | + public countryChanging(e: ISimpleComboSelectionChangingEventArgs, cell) { |
| 32 | + const ID = cell.row.data.ID; |
| 33 | + const nextRegionCombo = this.combos.filter( |
| 34 | + (combo) => combo.id === 'region-' + ID |
| 35 | + )[0]; |
| 36 | + const nextCityCombo = this.combos.filter( |
| 37 | + (combo) => combo.id === 'city-' + ID |
| 38 | + )[0]; |
| 39 | + this.clearOldData(cell, nextRegionCombo, nextCityCombo); |
| 40 | + this.selectedCountry = e.newSelection as Country; |
| 41 | + cell.update(e.newSelection ? e.newSelection : ''); |
| 42 | + if (e.newSelection) { |
| 43 | + document.getElementById('region-progress-' + ID).style.visibility = 'visible'; |
| 44 | + this.loadingTime = 2000; |
| 45 | + } |
| 46 | + setTimeout(() => { |
| 47 | + nextRegionCombo.data = getCitiesByCountry([this.selectedCountry?.name]) |
| 48 | + .map((c) => ({ name: c.region, country: c.country })) |
| 49 | + .filter((v, i, a) => a.findIndex((r) => r.name === v.name) === i); |
| 50 | + document.getElementById('region-progress-' + ID).style.visibility = 'hidden'; |
| 51 | + }, this.loadingTime); |
| 52 | + this.selectedRegion = null; |
| 53 | + this.selectedCity = null; |
| 54 | + this.loadingTime = 0; |
| 55 | + } |
| 56 | + |
| 57 | + public regionChanging(e: ISimpleComboSelectionChangingEventArgs, cell) { |
| 58 | + const nextComboID = 'city-' + cell.row.data.ID; |
| 59 | + const cityCombo = this.combos.filter( |
| 60 | + (combo) => combo.id === nextComboID |
| 61 | + )[0]; |
| 62 | + this.clearOldData(cell, null, cityCombo); |
| 63 | + |
| 64 | + this.selectedRegion = e.newSelection as Region; |
| 65 | + cell.update(e.newSelection ? e.newSelection : ''); |
| 66 | + if (e.newSelection) { |
| 67 | + document.getElementById( |
| 68 | + 'city-progress-' + cell.row.data.ID |
| 69 | + ).style.visibility = 'visible'; |
| 70 | + this.loadingTime = 2000; |
| 71 | + } |
| 72 | + setTimeout(() => { |
| 73 | + cityCombo.data = getCitiesByCountry([this.selectedCountry?.name]).filter( |
| 74 | + (c) => c.region === this.selectedRegion?.name |
| 75 | + ); |
| 76 | + document.getElementById( |
| 77 | + 'city-progress-' + cell.row.data.ID |
| 78 | + ).style.visibility = 'hidden'; |
| 79 | + }, this.loadingTime); |
| 80 | + this.selectedCity = null; |
| 81 | + this.loadingTime = 0; |
| 82 | + } |
| 83 | + |
| 84 | + private clearOldData(cell, RegionCombo, CityCombo) { |
| 85 | + const nextCellIndex = cell.column.visibleIndex + 1; |
| 86 | + cell.row.cells[nextCellIndex].update(''); |
| 87 | + |
| 88 | + if (CityCombo !== null) CityCombo.data = []; |
| 89 | + if (RegionCombo !== null) { |
| 90 | + RegionCombo.data = []; |
| 91 | + cell.row.cells[nextCellIndex + 1].update(''); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments