File

src/app/search-ui/facets/category/category.component.ts

Implements

OnInit

Metadata

selector app-category
styleUrls category.component.css
templateUrl ./category.component.html

Index

Methods
Inputs
Outputs

Constructor

constructor()

Inputs

categories

Type: Category[]

Outputs

selectionChanged $event type: EventEmitter<Category>

Methods

ngOnInit
ngOnInit()
Returns : void
onSelectionChanged
onSelectionChanged(category: Category)

Emits a Category on selection change.

Parameters :
Name Type Optional Description
category Category no

The category to be emitted.

Returns : void
toggleSelected
toggleSelected(category: Category)

Select or unselect a Category. Trick: we take advantage of the existing "isRefined" property as a selection flag.

Parameters :
Name Type Optional Description
category Category no

The category to be selected/unselected.

Returns : void
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { take } from 'rxjs/internal/operators/take';

export interface Category {
  name: string;
  count: number;
  isRefined: boolean;
  _dirty?: boolean;
}

@Component({
  selector: 'app-category',
  templateUrl: './category.component.html',
  styleUrls: ['./category.component.css']
})
export class CategoryComponent implements OnInit {
  @Input() categories: Category[];
  @Output() selectionChanged: EventEmitter<Category>;

  constructor() {
    this.selectionChanged = new EventEmitter();
  }

  ngOnInit() {}

  /**
   * Emits a Category on selection change.
   * @param category The category to be emitted.
   */
  onSelectionChanged(category: Category) {
    if (category._dirty) {
      this.selectionChanged.emit(category);
    }
  }

  /**
   * Select or unselect a Category. Trick: we take advantage of the existing "isRefined" property
   * as a selection flag.
   * @param category The category to be selected/unselected.
   */
  toggleSelected(category: Category) {
    category._dirty = true;
    category.isRefined = !category.isRefined;
  }
}
<mat-chip-list [multiple]="true" selectable="true">
  <mat-chip *ngFor="let category of categories" (click)="toggleSelected(category)" [selected]="category.isRefined" [selectable]="true"
    (selectionChange)="onSelectionChanged(category)" [value]="category.name">{{ category.name }} ({{ category.count }})</mat-chip>
</mat-chip-list>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""