rsanchez
2017-03-21 a6e1ace2b6bdba8c08a4acfa42433f3ac073b747
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { AfterViewInit, Component, Input, ViewChild } from '@angular/core';
import { TdDataTableService, TdPagingBarComponent, TdDataTableSortingOrder, IPageChangeEvent, ITdDataTableSortChangeEvent } from '@covalent/core';
import { UserService } from '../user.service';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
export interface IListing {
   create() : void;
   edit(id: number | string) : void;
   refresh() : void;
   sort(sortEvent: ITdDataTableSortChangeEvent): void;
   search(searchTerm: string): void;
   page(pagingEvent: IPageChangeEvent): void;
}
export class ListingBase implements IListing, AfterViewInit {
  data: any[] = [];
  
  @ViewChild('pagingBar') pagingBar : TdPagingBarComponent ;
  filteredData: any[] = this.data;
  filteredTotal: number = this.data.length;
  
  searchTerm: string = '';
  fromRow: number = 1;
  currentPage: number = 1;
  pageSize: number = 10;
  sortBy: string;
  sortOrder: TdDataTableSortingOrder = TdDataTableSortingOrder.Descending;
  filteredItems = this.data.length;
  
  constructor(private _dataTableService: TdDataTableService) {
  }
  create() : void {
    // To be implemented in the child class
    // For instance: 
    // this.router.navigate([`create`], {relativeTo: this.route});
    throw new Error('Method not implemented');
  }
  edit(id: number | string) : void {
    // To be implemented in the child class
    // For instance: 
    // this.router.navigate([`edit/${id}`], {relativeTo: this.route});
    throw new Error('Method not implemented');
  }
  sort(sortEvent: ITdDataTableSortChangeEvent): void {
    this.sortBy = sortEvent.name;
    this.sortOrder = sortEvent.order;
    this.refresh();
  }
  search(searchTerm: string): void {
    this.searchTerm = searchTerm;
    this.refresh();
  }
  page(pagingEvent: IPageChangeEvent): void {
    this.fromRow = pagingEvent.fromRow;
    this.currentPage = pagingEvent.page;
    this.pageSize = pagingEvent.pageSize;
    this.refresh();
  }
  refresh(): void {
    let newData: any[] = this.data;
    newData = this._dataTableService.filterData(newData, this.searchTerm, true);
    this.filteredTotal = newData.length;
    this.filteredItems = newData.length;
    newData = this._dataTableService.sortData(newData, this.sortBy, this.sortOrder);
    newData = this._dataTableService.pageData(newData, this.fromRow, this.currentPage * this.pageSize);
    this.filteredData = newData;
  }
  ngAfterViewInit(): void {
    this.refresh();
  }
  
}