I am using Angular material 2 multi select with Angular 5. I have added a search filter to it but when user searches for particular item and select those then it removes the items which was selected before.
I want to retain all the selected items unless and until the user deselect them.
Eg: I have a list of regions, if user selects Africa and Asia then searches and selects for Europe so we only see Europe as selected.
filterRegion.html
<mat-select [compareWith]="compareFn" placeholder="REGION" [formControl]="region" multiple>
<mat-select-trigger>
{{region.value ? region.value[0]?.value : ''}}
<span *ngIf="region.value?.length > 1" >
(+{{region.value.length - 1}})
</span>
</mat-select-trigger>
<mat-form-field class="searchBox">
<input matInput placeholder="Search" [(ngModel)]="searchRegion" >
</mat-form-field>
<mat-option *ngFor="let r of regionList | filterSearch: searchRegion" [value]="r">{{r.value}}</mat-option>
</mat-select>
filter-search-pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filterSearch'
})
export class FilterSearchPipe implements PipeTransform {
transform(value: any, input: string): any {
if (input) {
input = input.toLowerCase();
return value.filter(function (el: Object) {
return el['value'].toLowerCase().indexOf(input) > -1;
})
}
return value;
}
}
I tried using this : https://github.com/albyrock87/material2/blob/5c196ad65d1bd5d8cb02a6bd78407ee2ef5be198/src/demo-app/select/select-demo.html
But i was getting error for mat-select-header and mat-select-search.