1 |
58825
|
stefania.m
|
import { Component, Input, OnChanges } from '@angular/core';
|
2 |
|
|
import { CountryTableData } from '../../domain/overview-map-data';
|
3 |
57592
|
stefania.m
|
|
4 |
|
|
@Component({
|
5 |
|
|
selector: 'app-countries-table',
|
6 |
|
|
templateUrl: './countries-table.component.html',
|
7 |
|
|
})
|
8 |
|
|
|
9 |
57893
|
stefania.m
|
export class CountriesTableComponent implements OnChanges {
|
10 |
57592
|
stefania.m
|
|
11 |
|
|
@Input() isPercentage: boolean;
|
12 |
58825
|
stefania.m
|
@Input() countries: CountryTableData[];
|
13 |
57592
|
stefania.m
|
|
14 |
|
|
isSortedBy: string;
|
15 |
|
|
isDescending: boolean = true;
|
16 |
|
|
|
17 |
|
|
constructor() {}
|
18 |
|
|
|
19 |
57893
|
stefania.m
|
ngOnChanges() {
|
20 |
58825
|
stefania.m
|
this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
|
21 |
57893
|
stefania.m
|
}
|
22 |
|
|
|
23 |
57592
|
stefania.m
|
sortBy(field: string) {
|
24 |
|
|
|
25 |
|
|
if (field === this.isSortedBy) {
|
26 |
|
|
this.isDescending = !this.isDescending;
|
27 |
|
|
} else {
|
28 |
|
|
this.isDescending = true;
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
this.isSortedBy = field;
|
32 |
|
|
|
33 |
|
|
if (field !== 'country') {
|
34 |
58825
|
stefania.m
|
if (this.isDescending) {
|
35 |
|
|
this.countries.sort((a, b) => b[field] - a[field]);
|
36 |
57592
|
stefania.m
|
} else {
|
37 |
58825
|
stefania.m
|
this.countries.sort((a, b) => a[field] - b[field]);
|
38 |
57592
|
stefania.m
|
}
|
39 |
|
|
} else {
|
40 |
|
|
if (this.isDescending) {
|
41 |
58825
|
stefania.m
|
this.countries.sort((a, b) => (a['name'] < b['name']) ? 1 : -1);
|
42 |
57592
|
stefania.m
|
} else {
|
43 |
58825
|
stefania.m
|
this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
|
44 |
57592
|
stefania.m
|
}
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
}
|
48 |
|
|
}
|