Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MatTableMediator<F, O>

This mediator class takes care of connecting and managing an Angular Material table with pagination, sorting and data from an observable. It takes care of:

  • firing the fetch function when the trigger$, sorting or page changes
  • resetting page index to 0, if trigger$ or sorting changes
  • error handling (retries or emits error$ and returns empty data array)
  • indicating loading
  • completing all observables, if ngOnDestroy() is called

After creating an object of the mediator and calling the start method, it will automatically be ready to load the data, as soon as the trigger$ observable emits.

see

start

Type parameters

  • F

    type of the trigger payload to be used in the underlying fetch function, for example a form output

  • O

    type of the output data for the table. This must match MatTable's generic type

Hierarchy

Implements

  • OnDestroy

Index

Constructors

Protected constructor

  • Creates a new instance of a MatTableFetchMediator and calls its ngOnInit function.

    see

    MatTableMediator

    Parameters

    • table: MatTable<O>

      Reference to the MatTable

    • Optional paginator: MatPaginator

      Reference to the MatPaginator

    • Optional sort: MatSort

      Reference to the MatSort

    • Default value config: Partial<MediatorConfig<O>> = {}

    Returns MatTableMediator

Properties

Protected _data$

_data$: Subject<O[]> = new Subject<Array<O>>()

The data from the MediatorData object. It only contains the received entities and will be fed into the table.

Protected _destroy$

_destroy$: Subject<void> = new Subject<void>()

A subject for cleanups. Will be called and completed by the ngOnDestroy method.

see

ngOnDestroy

Protected _error$

_error$: Subject<Error> = new Subject<Error>()

The subject that receives any error from the fetching process.

see

[[handleError

Protected _loading$

_loading$: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false)

The BehaviorSubject indicating the loading status.

Protected _mediatorData$

_mediatorData$: Observable<MediatorData<O>>

The raw MediatorData observable, resulting from the createDataFetch method.

see

createDataFetch

see

_data$

Protected _totalResults$

_totalResults$: Subject<number> = new Subject<number>()

The total result count, taken from the MediatorData object.

Protected config

config: MediatorConfig<O>

A frozen MediatorConfig, put together in the constructor. By default the MatTableMediator.DEFAULT_CONFIG values will be used.

see

MatTableMediator.DEFAULT_CONFIG

onPageReset$

onPageReset$: Observable<[EventEmitter<Sort>, F] | any>

An observable that emits, whenever the page is reset. To change the behaviour overwrite the createOnPageReset method.

see

createOnPageReset

Protected Optional paginator

paginator: MatPaginator

Reference to the MatPaginator

Protected Optional sort

sort: MatSort

Reference to the MatSort

Protected table

table: MatTable<O>

Reference to the MatTable

Accessors

data$

  • get data$(): Observable<Array<O>>
  • An observable of the data, which gets fed into the table. The mediator takes care of feeding the table. You can use this property for additional actions.

    Returns Observable<Array<O>>

error$

  • get error$(): Observable<Error>
  • An observable with the latest error. It does NOT return undefined, when a fetch was successful.

    Returns Observable<Error>

isLoading$

  • get isLoading$(): Observable<boolean>
  • An observable which indicates loading. by default loading starts when the fetch function gets triggered and ends when data was received and just before the table and paginator gets fed.

    Returns Observable<boolean>

onFetchBegin$

  • get onFetchBegin$(): Observable<void>
  • An observable which only emits if loading has started. You may use this to hide an error message or perform other actions.

    Returns Observable<void>

onNoResultsFound$

  • get onNoResultsFound$(): Observable<void>
  • An observable which only emits if no results were found.

    It's a filter for the resultsLength$ property with x <= 0.

    Returns Observable<void>

onResultsFound$

  • get onResultsFound$(): Observable<number>
  • Returns an observable which only emits if results were found.

    It's a filter for the resultsLength$ property with x > 0.

    Returns Observable<number>

Protected page$

  • get page$(): EventEmitter<PageEvent | undefined>
  • safely returns this.paginator.page.pipe(startWith({})) or of(undefined), if MatPaginator object wasn't provided

    Returns EventEmitter<PageEvent | undefined>

Protected pageIndex

  • get pageIndex(): number | undefined
  • safely returns this.paginator.pageIndex or undefined, if MatPaginator object wasn't provided

    Returns number | undefined

Protected pageSize

  • get pageSize(): number | undefined
  • safely returns this.paginator.pageSize or undefined, if MatPaginator object wasn't provided

    Returns number | undefined

Protected sortActive

  • get sortActive(): Column<O> | undefined

Protected sortChange$

  • get sortChange$(): EventEmitter<Sort>
  • safely returns this.sort.sortChange.pipe(startWith({})) or of(undefined), if MatSort object wasn't provided

    Returns EventEmitter<Sort>

Protected sortDirection

  • get sortDirection(): SortDirection | undefined
  • safely returns this.sort.direction or undefined, if MatSort object wasn't provided

    Returns SortDirection | undefined

totalResults$

  • get totalResults$(): Observable<number>
  • An observable which emits the total count of results that are available on the server. The mediator takes care of feeding the paginator. You can use this property for additional actions.

    Returns Observable<number>

Protected trigger$

  • the observable whose latest value always gets fed into the fetch function If you don't need this please use the following example in your class

    example

    protected trigger$ = of(undefined); // start instantly protected trigger$ = new ReplaySubject(1); // only on a button click protected trigger$ = new BehaviourSubject(undefined); // start instantly and on every other button click

    Returns TriggerPayload<F>

Methods

Protected createDataFetch

  • This is the mediator's core function and setups the logic. It merges the relevant observables, starts the fetch function and maps the values to fit the mediator's needs.

    Returns Observable<MediatorData<O>>

Protected createOnPageReset

  • createOnPageReset(): Observable<[EventEmitter<Sort>, F]> | Observable<any>
  • This function creates an internal observable to reset the paginator. By default it merges sortChange$ and the trigger$ observable.

    see

    sortChange$

    see

    trigger$

    Returns Observable<[EventEmitter<Sort>, F]> | Observable<any>

Abstract fetch

  • fetch(payload?: F, sortBy?: Column<O>, sortDirection?: SortDirection, pageIndex?: number, pageSize?: number): Observable<MediatorData<O>>
  • The function which is used for communicating with the API.

    Parameters

    • Optional payload: F

      the latest trigger payload

    • Optional sortBy: Column<O>

      the currently selected column

    • Optional sortDirection: SortDirection

      "asc", "desc" or ""

    • Optional pageIndex: number

      the current page the user is on

    • Optional pageSize: number

      the given page size by the user

    Returns Observable<MediatorData<O>>

Protected handleError

  • This function handles any errors that occur while fetching the data. You can either safely handle the error and return replacement data or rethrow the error. Throwing an error will complete the observable and would have to be started again by calling #initDataFetch

    Parameters

    • error: Error

      The thrown error

    Returns Observable<MediatorData<O>>

    The replacement data as an observable (e.g. return of({ total: -1, data: [] });)

Protected handlePageReset

  • handlePageReset(value: [EventEmitter<Sort>, F] | any): void
  • Handler that gets called whenever the onPageReset$ observable emits.

    see

    onPageReset$

    Parameters

    • value: [EventEmitter<Sort>, F] | any

      the result of the onPageReset$ observable

    Returns void

Protected handleResult

  • This function gets called every time new data was fetched. It's responsible for feeding the data into the table and paginator. It also controls the subjects for the properties like data$, resultsLength$ and isLoading$

    Parameters

    • result: MediatorData<O>

      the result of the fetchFn, already mapped to MediatorData

    Returns void

ngOnDestroy

  • ngOnDestroy(): void
  • Completes all created observables so there are no memory leaks.

    You have to call this manually!

    Returns void

start

  • start(): void
  • This function initialises the page reset and the fetch function, effectively starting the mediator. It won't be called automatically. You may want to call it at the end of your subclass' constructor.

    this.onPageReset$ = this.createOnPageReset();
    this.onPageReset$.subscribe(value => this.handlePageReset(value));
    // within this method any errors will be forwarded to `handleError`
    this._mediatorData$ = this.createDataFetch();
    this._mediatorData$.subscribe(result => this.handleResult(result));

    Returns void

Object literals

Static DEFAULT_CONFIG

DEFAULT_CONFIG: object

The default config for mediators.

{
  trackByFn: index => index, // track by index
  attempts: 0,               // do not retry
  debounceLoading: 150       // debounce loading indicator by 150ms
}
see

MediatorConfig

attempts

attempts: number = 0

debounceLoading

debounceLoading: number = 150

trackByFn

  • trackByFn(index: number): number

Generated using TypeDoc