I have this autocomplete in Angular
JavaScript
x
9
1
<input type="text" matInput [ngModel]="List" #searchInput [matAutocomplete]="autocopmlete" (focus)="filter('')"
2
(ngModelChange)="filter($event)">
3
<mat-autocomplete #autocopmlete="matAutocomplete" [displayWith]="Name">
4
<mat-option *ngFor="let item of filteredObject" [value]="item" (onSelectionChange)="selectUser($event,item)"
5
(click)="searchInput.value=''">
6
{{ item.Name }}
7
</mat-option>
8
</mat-autocomplete>
9
we use this filter function
JavaScript
1
9
1
filter(value = '') {
2
if (typeof value !== 'string' || typeof value === 'undefined' || typeof this.chatList==='undefined') {
3
return;
4
}
5
this.filteredObject = this.chatList.filter(
6
a => ~a.Name.toLocaleLowerCase().indexOf(value.toLocaleLowerCase()))
7
);
8
}
9
but issue is if i get those records also where Name
is blank how should i remove that blank record from result.
Advertisement
Answer
You would need to add an additional check in the filter
function, where you can check if Name
property exist on object !!a.Name
JavaScript
1
7
1
this.filteredObject = this.chatList.filter(
2
a => (
3
!!a.Name &&
4
~a.Name.toLocaleLowerCase().indexOf(value.toLocaleLowerCase())
5
)
6
);
7