【问题标题】:How to search multiple fields in ng-select?如何在 ng-select 中搜索多个字段?
【发布时间】:2019-06-11 21:08:20
【问题描述】:

我想在 ng-select 下拉菜单中合并两个 API 字段“code 和 name”。 例如:-

  Code       : MI

  name       : MI 3sPrime

 Format      : MI - MI 3sPrime

我使用下面的代码进行下拉

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;
 }

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

下面是图表

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

搜索应该应用 code - name..这意味着当我输入 MI - 时,过滤必须起作用。 有什么方法吗?有人可以帮帮我吗?

【问题讨论】:

    标签: angular6 angular5 angular7 angular-ngselect


    【解决方案1】:

    试试这个

      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;
     }
    

    【讨论】:

    • 如果要定义“this”,请使用箭头函数。 customSearchFn = (term: string, item: any) =&gt; { // Can use this.something here }
    • 在 html ->
    猜你喜欢
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2019-01-26
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多