1
I installed the ngx-mat-select-search and followed the example of Stackblitz.
The example worked perfectly, just as it is in the stackblitz. After that I changed my code to load the data dynamically. I loaded the data normally and the values are listed in the UI, but the filter bar does nothing and no error occurs.
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)
);
}
}
places a ngif technicalRooms to wait for it to load
– Eduardo Vargas
@Eduardovargas, I did what you suggested and made the mistake
Cannot set property 'compareWith' of undefined
– George Wurthmann
vc fez *ngIf="technicalRooms"?
– Eduardo Vargas
Yes, that’s right. I’ve already solved, if no one answers I put my solution here.
– George Wurthmann