Projekt

Obecné

Profil

Stáhnout (2.36 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { DataSource } from '@angular/cdk/collections';
2
import { MatPaginator } from '@angular/material/paginator';
3
import { MatSort } from '@angular/material/sort';
4
import { map } from 'rxjs/operators';
5
import { Observable, of as observableOf, merge } from 'rxjs';
6
import { TextRegion } from '../../../../model/parts/TextRegion';
7

    
8
/**
9
 * Data source for the SearchPaginator view. This class should
10
 * encapsulate all logic for fetching and manipulating the displayed data
11
 * (including sorting, pagination, and filtering).
12
 */
13
export class SearchPaginatorDataSource extends DataSource<TextRegion> {
14
  data: TextRegion[];
15
  paginator: MatPaginator;
16
  sort: MatSort;
17

    
18
  constructor() {
19
    super();
20
  }
21

    
22
  /**
23
   * Connect this data source to the table. The table will only update when
24
   * the returned stream emits new items.
25
   * @returns A stream of the items to be rendered.
26
   */
27
  connect(): Observable<TextRegion[]> {
28
    // Combine everything that affects the rendered data into one update
29
    // stream for the data-table to consume.
30
    const dataMutations = [
31
      observableOf(this.data),
32
      this.paginator.page,
33
      this.sort.sortChange
34
    ];
35

    
36
    return merge(...dataMutations).pipe(map(() => {
37
      return this.getPagedData(this.getSortedData([...this.data]));
38
    }));
39
  }
40

    
41
  /**
42
   *  Called when the table is being destroyed. Use this function, to clean up
43
   * any open connections or free any held resources that were set up during connect.
44
   */
45
  disconnect() {}
46

    
47
  /**
48
   * Paginate the data (client-side). If you're using server-side pagination,
49
   * this would be replaced by requesting the appropriate data from the server.
50
   */
51
  private getPagedData(data: TextRegion[]) {
52
    const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
53
    return data.splice(startIndex, this.paginator.pageSize);
54
  }
55

    
56
  /**
57
   * Sort the data (client-side). If you're using server-side sorting,
58
   * this would be replaced by requesting the appropriate data from the server.
59
   */
60
  private getSortedData(data: TextRegion[]) {
61
    if (!this.sort.active || this.sort.direction === '') {
62
      return data;
63
    }
64

    
65
    return data.sort((a, b) => {
66
      return 0;
67
    });
68
  }
69
}
70

    
71
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
72
function compare(a: string | number, b: string | number, isAsc: boolean) {
73
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
74
}
(1-1/5)