I have installed ngx-mat-select-search and followed the example from StackBlitz.
The example worked fine, like in stackblitz. Then, I changed the code to use my dynamic data. I loaded the data normally and the values are listed in the UI (the searchble dropdown), but the search bar do nothing and there is no error.
HTML:
<mat-form-field class="selectList-full-width">
<mat-select [formControl]="selectTechnicalRoomCtrl" [placeholder]="'SelectTechnicalRoom' | localize" #singleSelect>
<mat-option>
<ngx-mat-select-search [formControl]="selectTechnicalRoomFilterCtrl" [placeholderLabel]="'Search' | localize"></ngx-mat-select-search>
</mat-option>
<mat-option *ngFor="let technicalRoom of technicalRooms" [value]="technicalRoom">
{{technicalRoom.nameRoom}}
</mat-option>
</mat-select>
</mat-form-field>
<div class="p-r-25 btn-toolbar">
<button style="margin-left: 10px; padding: 0 1em;" mat-raised-button color="primary" >{{ 'ListPoints' | localize }}</button>
</div>
TS:
export class HomeComponent extends AppComponentBase implements OnInit, AfterViewInit, OnDestroy {
technicalRooms: IdNameTechnicalRoomDto[] = [];
public selectTechnicalRoomCtrl: FormControl = new FormControl();
public selectTechnicalRoomFilterCtrl: FormControl = new FormControl();
/** list of itens filtered by search keyword */
public filteredTechnicalRooms: ReplaySubject<IdNameTechnicalRoomDto[]> = new ReplaySubject<IdNameTechnicalRoomDto[]>(1);
@ViewChild('singleSelect', { static: true }) singleSelect: MatSelect;
protected _onDestroy = new Subject<void>();
constructor(
injector: Injector,
private _technicalRoomsService: TechnicalRoomServiceProxy,
) {
super(injector);
}
ngOnInit() {
this.list();
// set initial selection
this.selectTechnicalRoomCtrl.setValue(this.technicalRooms[10]);
// load the initial itens list
this.filteredTechnicalRooms.next(this.technicalRooms.slice());
// listen for search field value changes
this.selectTechnicalRoomFilterCtrl.valueChanges
.pipe(takeUntil(this._onDestroy))
.subscribe(() => {
this.filterTechnicalRooms();
});
}
list(): void {
this._technicalRoomsService
.getList()
.pipe()
.subscribe(
data => this.technicalRooms = data["result"]
);
}
ngAfterViewInit() {
this.setInitialValue();
}
ngOnDestroy() {
this._onDestroy.next();
this._onDestroy.complete();
}
/**
* Sets the initial value after the filteredTechnicalRooms are loaded initially
*/
protected setInitialValue() {
this.filteredTechnicalRooms
.pipe(take(1), takeUntil(this._onDestroy))
.subscribe(() => {
this.singleSelect.compareWith = (a: IdNameTechnicalRoomDto, b: IdNameTechnicalRoomDto) => a && b && a.id === b.id;
});
}
protected filterTechnicalRooms() {
if (!this.technicalRooms) {
return;
}
// get the search keyword
let search = this.selectTechnicalRoomFilterCtrl.value;
if (!search) {
this.filteredTechnicalRooms.next(this.technicalRooms.slice());
return;
} else {
search = search.toLowerCase();
}
// filter the technicalRooms
this.filteredTechnicalRooms.next(
this.technicalRooms.filter(technicalRoom => technicalRoom.nameRoom.toLowerCase().indexOf(search) > -1)
);
}
}
EDIT:
I added *ngIf="technicalRooms" to a div above mat-form-field but now I receive the error:
Cannot set property 'compareWith' of undefined