如何在 ng-select 中搜索多个字段?

Ang*_*eji 6 angular-ngselect angular5 angular6 angular7

我想在 ng-select 下拉列表中合并两个 API 字段“代码和名称”。例如:-

  Code       : MI

  name       : MI 3sPrime

 Format      : MI - MI 3sPrime
Run Code Online (Sandbox Code Playgroud)

我使用以下代码进行下拉

组件.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>
Run Code Online (Sandbox Code Playgroud)

组件.ts

  customSearchFn(term: string, item: any) {
    term = term.toLocaleLowerCase();
    return item.code.toLocaleLowerCase().indexOf(term) > -1 || 
    item.name.toLocaleLowerCase().indexOf(term) > -1;
 }
Run Code Online (Sandbox Code Playgroud)

搜索中:搜索时正在获取代码和名称。但我想搜索代码、名称和给定格式(代码 - 名称)

下面是图表

在此处输入图片说明

在这里,当我搜索“MI -”时,搜索不起作用

在此处输入图片说明

搜索应申请格式code - name ..这意味着当我输入MI - 时,过滤必须起作用。有什么方法吗?有谁能够帮助我?

ysf*_*ysf 6

尝试这个

  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;
 }
Run Code Online (Sandbox Code Playgroud)