Skip to content

Commit

Permalink
Merge branch 'develop' into server_side_filtering_example
Browse files Browse the repository at this point in the history
  • Loading branch information
macjohnny authored Jan 8, 2019
2 parents dace34b + bc619a6 commit d99bf47
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ <h3>Single selection with custom clear icon</h3>
<p>
Selected Bank: {{bankCtrl.value?.name}}
</p>

<h3>Single selection with option groups</h3>
<p>
<mat-form-field>
<mat-select [formControl]="bankGroupsCtrl" placeholder="Bank" #singleSelect>
<ngx-mat-select-search [formControl]="bankGroupsFilterCtrl"></ngx-mat-select-search>
<mat-optgroup *ngFor="let group of filteredBankGroups | async" [label]="group.name">
<mat-option *ngFor="let bank of group.banks" [value]="bank">
{{bank.name}}
</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
</p>
<p>
Selected Bank: {{bankGroupsCtrl.value?.name}}
</p>

<span class="version-info">Current build: {{version.full}}</span>
<h3>Server Side Search</h3>
<p>
Expand Down
105 changes: 105 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ interface Bank {
name: string;
}

interface BankGroup {
name: string;
banks: Bank[];
}

@Component({
selector: 'app-root',
templateUrl: 'app.component.html'
Expand All @@ -38,6 +43,12 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
/** control for filter for server side. */
public bankServerSideFilteringCtrl: FormControl = new FormControl();

/** control for the selected bank for option groups */
public bankGroupsCtrl: FormControl = new FormControl();

/** control for the MatSelect filter keyword for option groups */
public bankGroupsFilterCtrl: FormControl = new FormControl();

/** list of banks */
private banks: Bank[] = [
{name: 'Bank A (Switzerland)', id: 'A'},
Expand All @@ -60,6 +71,53 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
{name: 'Bank R (Germany)', id: 'R'}
];

/** list of bank groups */
private bankGroups: BankGroup[] = [
{
name: 'Switzerland',
banks: [
{name: 'Bank A', id: 'A'},
{name: 'Bank B', id: 'B'}
]
},
{
name: 'France',
banks: [
{name: 'Bank C', id: 'C'},
{name: 'Bank D', id: 'D'},
{name: 'Bank E', id: 'E'},
]
},
{
name: 'Italy',
banks: [
{name: 'Bank F', id: 'F'},
{name: 'Bank G', id: 'G'},
{name: 'Bank H', id: 'H'},
{name: 'Bank I', id: 'I'},
{name: 'Bank J', id: 'J'},
]
},
{
name: 'United States of America',
banks: [
{name: 'Bank Kolombia', id: 'K'},
]
},
{
name: 'Germany',
banks: [
{name: 'Bank L', id: 'L'},
{name: 'Bank M', id: 'M'},
{name: 'Bank N', id: 'N'},
{name: 'Bank O', id: 'O'},
{name: 'Bank P', id: 'P'},
{name: 'Bank Q', id: 'Q'},
{name: 'Bank R', id: 'R'}
]
}
];

/** list of banks filtered by search keyword */
public filteredBanks: ReplaySubject<Bank[]> = new ReplaySubject<Bank[]>(1);

Expand All @@ -69,6 +127,9 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
/** list of banks filtered after simulating server side search */
public filteredServerSideBanks: ReplaySubject<Bank[]> = new ReplaySubject<Bank[]>(1);

/** list of bank groups filtered by search keyword for option groups */
public filteredBankGroups: ReplaySubject<BankGroup[]> = new ReplaySubject<BankGroup[]>(1);

@ViewChild('singleSelect') singleSelect: MatSelect;
@ViewChild('multiSelect') multiSelect: MatSelect;

Expand All @@ -83,6 +144,7 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
// load the initial bank list
this.filteredBanks.next(this.banks.slice());
this.filteredBanksMulti.next(this.banks.slice());
this.filteredBankGroups.next(this.copyBankGroups(this.bankGroups));

// listen for search field value changes
this.bankFilterCtrl.valueChanges
Expand All @@ -95,6 +157,7 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
.subscribe(() => {
this.filterBanksMulti();
});

this.bankServerSideFilteringCtrl.valueChanges
.pipe(
takeUntil(this._onDestroy),
Expand All @@ -109,6 +172,13 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
delay(500) // add server simulation
)
.subscribe(this.filteredServerSideBanks);

this.bankGroupsFilterCtrl.valueChanges
.pipe(takeUntil(this._onDestroy))
.subscribe(() => {
this.filterBankGroups();
});

}

ngAfterViewInit() {
Expand Down Expand Up @@ -173,4 +243,39 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {
);
}

private filterBankGroups() {
if (!this.bankGroups) {
return;
}
// get the search keyword
let search = this.bankGroupsFilterCtrl.value;
const bankGroupsCopy = this.copyBankGroups(this.bankGroups);
if (!search) {
this.filteredBankGroups.next(bankGroupsCopy);
return;
} else {
search = search.toLowerCase();
}
// filter the banks
this.filteredBankGroups.next(
bankGroupsCopy.filter(bankGroup => {
const showBankGroup = bankGroup.name.toLowerCase().indexOf(search) > -1;
if (!showBankGroup) {
bankGroup.banks = bankGroup.banks.filter(bank => bank.name.toLowerCase().indexOf(search) > -1);
}
return bankGroup.banks.length > 0;
})
);
}

private copyBankGroups(bankGroups: BankGroup[]) {
const bankGroupsCopy = [];
bankGroups.forEach(bankGroup => {
bankGroupsCopy.push({
name: bankGroup.name,
banks: bankGroup.banks.slice()
});
});
return bankGroupsCopy;
}
}

0 comments on commit d99bf47

Please sign in to comment.