aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/rest/rest-table.ts
blob: 165fc4e45959fffe364ac23ced8e032476528494 (plain) (blame)
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
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'
import { SortMeta } from 'primeng/components/common/sortmeta'

import { RestPagination } from './rest-pagination'

export abstract class RestTable {

  abstract totalRecords: number
  abstract rowsPerPage: number
  abstract sort: SortMeta
  abstract pagination: RestPagination

  private sortLocalStorageKey = 'rest-table-sort-' + this.constructor.name

  protected abstract loadData (): void

  loadSort () {
    const result = localStorage.getItem(this.sortLocalStorageKey)

    if (result) {
      try {
        this.sort = JSON.parse(result)
      } catch (err) {
        console.error('Cannot load sort of local storage key ' + this.sortLocalStorageKey, err)
      }
    }
  }

  loadLazy (event: LazyLoadEvent) {
    this.sort = {
      order: event.sortOrder,
      field: event.sortField
    }

    this.pagination = {
      start: event.first,
      count: this.rowsPerPage
    }

    this.loadData()
    this.saveSort()
  }

  saveSort () {
    localStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort))
  }

}