File

src/lib/select-row/select-row.service.ts

Index

Properties
Methods
Accessors

Constructor

constructor(options: SelectRowOptions<Data> | null)
Parameters :
Name Type Optional
options SelectRowOptions<Data> | null No

Methods

clear
clear()
Returns : void
compareWith
compareWith(a: Data, b: Data)
Parameters :
Name Type Optional
a Data No
b Data No
Returns : boolean

Properties

Public selectedRows$
Type : Observable<Data[]>
Public Readonly selectionModel
Default value : new SelectionModel<Data>(true)

Accessors

selectedRows
getselectedRows()
import {
  Inject,
  Injectable,
  Optional,
} from '@angular/core';
import {
  getIdentifierPropertyValue,
  hasIdentifierProperty,
} from '@rxap/utilities';
import type { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { SelectRowOptions } from './select-row.options';
import { SelectionModel } from './selection-model';
import { RXAP_MATERIAL_TABLE_SYSTEM_SELECT_ROW_OPTIONS } from './tokens';

@Injectable()
export class SelectRowService<Data = unknown> {
  public selectedRows$: Observable<Data[]>;

  public readonly selectionModel = new SelectionModel<Data>(true);

  public get selectedRows(): Data[] {
    return this.selectionModel.selected;
  }

  constructor(
    @Optional()
    @Inject(RXAP_MATERIAL_TABLE_SYSTEM_SELECT_ROW_OPTIONS)
      options: SelectRowOptions<Data> | null = null,
  ) {
    this.selectionModel = new SelectionModel<Data>(
      options?.multiple,
      options?.selected,
      options?.emitChanges,
      options?.compareWith ?? this.compareWith,
    );
    this.selectedRows$ = this.selectionModel.changed.pipe(
      map(() => this.selectionModel.selected),
    );
  }

  clear() {
    this.selectionModel.clear();
  }

  compareWith(a: Data, b: Data): boolean {
    if (a === b) {
      return true;
    }
    if (hasIdentifierProperty(a) && hasIdentifierProperty(b)) {
      return getIdentifierPropertyValue(a) === getIdentifierPropertyValue(b);
    }
    return false;
  }

}

results matching ""

    No results matching ""