Project

General

Profile

1
import { Component, Input, OnChanges } from '@angular/core';
2
import { CountryTableData } from '../../domain/overview-map-data';
3

    
4
@Component({
5
  selector: 'app-countries-table',
6
  templateUrl: './countries-table.component.html',
7
})
8

    
9
export class CountriesTableComponent implements OnChanges {
10

    
11
  @Input() isPercentage: boolean;
12
  @Input() countries: CountryTableData[];
13

    
14
  isSortedBy: string;
15
  isDescending: boolean = true;
16

    
17
  constructor() {}
18

    
19
  ngOnChanges() {
20
    this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
21
  }
22

    
23
  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
      if (this.isDescending) {
35
        this.countries.sort((a, b) => b[field] - a[field]);
36
      } else {
37
        this.countries.sort((a, b) => a[field] - b[field]);
38
      }
39
    } else {
40
      if (this.isDescending) {
41
        this.countries.sort((a, b) => (a['name'] < b['name']) ? 1 : -1);
42
      } else {
43
        this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
44
      }
45
    }
46

    
47
  }
48
}
(4-4/12)