diff --git a/src/app/app.component.html b/src/app/app.component.html index fdd4ed9..1ee1e73 100755 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -79,4 +79,21 @@

Single selection with option groups

Current build: {{version.full}} +

Server Side Search

+

+ + + + + + + {{bank.name}} + + + +

+

+ Selected Bank: {{bankServerSideCtrl.value?.name}} +

+ diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 95cbf9c..cce81c0 100755 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -5,8 +5,7 @@ import {MatSelect} from '@angular/material'; import { ReplaySubject } from 'rxjs'; import { Subject } from 'rxjs'; -import { take } from 'rxjs/operators'; -import { takeUntil } from 'rxjs/operators'; +import { takeUntil, delay, filter, map, take, debounceTime } from 'rxjs/operators'; interface Bank { id: string; @@ -38,6 +37,12 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy { /** control for the MatSelect filter keyword multi-selection */ public bankMultiFilterCtrl: FormControl = new FormControl(); + /** control for the selected bank for server side filtering */ + public bankServerSideCtrl: FormControl = new FormControl(); + + /** control for filter for server side. */ + public bankServerSideFilteringCtrl: FormControl = new FormControl(); + /** control for the selected bank for option groups */ public bankGroupsCtrl: FormControl = new FormControl(); @@ -119,6 +124,9 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy { /** list of banks filtered by search keyword for multi-selection */ public filteredBanksMulti: ReplaySubject = new ReplaySubject(1); + /** list of banks filtered after simulating server side search */ + public filteredServerSideBanks: ReplaySubject = new ReplaySubject(1); + /** list of bank groups filtered by search keyword for option groups */ public filteredBankGroups: ReplaySubject = new ReplaySubject(1); @@ -149,11 +157,28 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy { .subscribe(() => { this.filterBanksMulti(); }); + + this.bankServerSideFilteringCtrl.valueChanges + .pipe( + takeUntil(this._onDestroy), + debounceTime(200), + filter(search => !!search), + map(search => { + if (!this.banks) { + return []; + } + return this.banks.filter(bank => bank.name.toLowerCase().indexOf(search) > -1); + }), + delay(500) // add server simulation + ) + .subscribe(this.filteredServerSideBanks); + this.bankGroupsFilterCtrl.valueChanges .pipe(takeUntil(this._onDestroy)) .subscribe(() => { this.filterBankGroups(); }); + } ngAfterViewInit() { diff --git a/src/styles.scss b/src/styles.scss index 99d0ed8..d3ba66c 100644 --- a/src/styles.scss +++ b/src/styles.scss @@ -16,3 +16,9 @@ body { font-size: 8pt; float: right; } + +.mat-option[aria-disabled=true].contains-select-search { + /* let move mat-select-search at the top of the dropdown. As option is disabled, there will be no-ripple hence safe. */ + position: static; + padding: 0; +}