Project

General

Profile

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

    
4
@Component({
5
  selector: 'app-countries-table',
6
  templateUrl: './countries-table.component.html',
7
  // styleUrls: ['./top-menu.component.css'],
8
  encapsulation: ViewEncapsulation.None
9
})
10

    
11
export class CountriesTableComponent implements OnChanges {
12

    
13
  @Input() isPercentage: boolean;
14
  @Input() type: string;
15
  @Input() countries: CountryOverview[];
16

    
17
  isSortedBy: string;
18
  isDescending: boolean = true;
19

    
20
  constructor() {}
21

    
22
  ngOnChanges() {
23
    this.countries.sort((a, b) => (a['country'] > b['country']) ? 1 : -1);
24
  }
25

    
26
  sortBy(field: string) {
27

    
28
    if (field === this.isSortedBy) {
29
      this.isDescending = !this.isDescending;
30
    } else {
31
      this.isDescending = true;
32
    }
33

    
34
    this.isSortedBy = field;
35

    
36
    if (field !== 'country') {
37
      if (this.isPercentage) {
38
        if (this.isDescending) {
39
          this.countries.sort((a, b) => b[field].percentage - a[field].percentage);
40
        } else {
41
          this.countries.sort((a, b) => a[field].percentage - b[field].percentage);
42
        }
43
      } else {
44
        if (this.isDescending) {
45
          this.countries.sort((a, b) => b[field].oa - a[field].oa);
46
        } else {
47
          this.countries.sort((a, b) => a[field].oa - b[field].oa);
48
        }
49
      }
50
    } else {
51
      if (this.isDescending) {
52
        this.countries.sort((a, b) => (a[field] < b[field]) ? 1 : -1);
53
      } else {
54
        this.countries.sort((a, b) => (a[field] > b[field]) ? 1 : -1);
55
      }
56
    }
57

    
58
  }
59
}
(4-4/10)