Project

General

Profile

1 57592 stefania.m
import { Component, Input, 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 {
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
  sortBy(field: string) {
23
24
    if (field === this.isSortedBy) {
25
      this.isDescending = !this.isDescending;
26
    } else {
27
      this.isDescending = true;
28
    }
29
30
    this.isSortedBy = field;
31
32
    if (field !== 'country') {
33
      if (this.isPercentage) {
34
        if (this.isDescending) {
35
          this.countries.sort((a, b) => b[field].percentage - a[field].percentage);
36
        } else {
37
          this.countries.sort((a, b) => a[field].percentage - b[field].percentage);
38
        }
39
      } else {
40
        if (this.isDescending) {
41
          this.countries.sort((a, b) => b[field].oa - a[field].oa);
42
        } else {
43
          this.countries.sort((a, b) => a[field].oa - b[field].oa);
44
        }
45
      }
46
    } else {
47
      if (this.isDescending) {
48
        this.countries.sort((a, b) => (a[field] < b[field]) ? 1 : -1);
49
      } else {
50
        this.countries.sort((a, b) => (a[field] > b[field]) ? 1 : -1);
51
      }
52
    }
53
54
  }
55
}