I want to merge two API fields "code and name" in ng-select dropdown. For example:-
Code : MI
name : MI 3sPrime
Format : MI - MI 3sPrime
I used the below code for dropdown
Component.Html
<ng-select [items]="products" bindLabel="code" bindValue="id"
placeholder="Select Goods Receipt" clearAllText="Clear" formControlName="productId" [searchFn]="customSearchFn">
<ng-template ng-label-tmp let-item="item">
<span [ngOptionHighlight]="search">{{ item.code }} - {{ item.name }}</span>
</ng-template>
<ng-template ng-option-tmp let-item="item" let-search="searchTerm" let-index="index">
<span [ngOptionHighlight]="search">{{ item.code }} - {{ item.name }}</span>
</ng-template>
</ng-select>
Component.ts
customSearchFn(term: string, item: any) {
term = term.toLocaleLowerCase();
return item.code.toLocaleLowerCase().indexOf(term) > -1 ||
item.name.toLocaleLowerCase().indexOf(term) > -1;
}
Searching: Code and name is fetching while searching. But I want to search for code , name and given format(Code - name)
Below are the diagrams
Here, While I search the "MI -" , Searching not works
Searching should apply for the format code - name..Which means when I type MI -, filteration have to work. Is there any method? Can anybody help me?
try this
customSearchFn(term: string, item: any) {
term = term.toLocaleLowerCase();
return item.code.toLocaleLowerCase().indexOf(term) > -1 ||
item.name.toLocaleLowerCase().indexOf(term) > -1 ||
(item.code + " - " + item.name).toLocaleLowerCase().indexOf(term) > -1;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With