Angular - 自定义 ngx-bootstrap Typeahead 以根据以逗号分隔的多个值自动完成

标签 angular angular6 typeahead ngx-bootstrap

我有一个预先输入自动完成输入框。我的要求是如果列表与以逗号输入的字符串列表匹配,则显示列表。

例如:

如果我的列表是:

[{
        deviceId: '1111111',
        name: 'Crafty'
    },
    {
        deviceId: '000000',
        name: 'TeleCom'
    },
    {
        deviceId: '1110009999',
        name: 'TeleCom'
    }
    {
        deviceId: '999999',
        name: 'Mobile'
    }
 ]

如果我在搜索框中输入“111, 999”,自动完成列表应显示 3 项(包含 111、999 或两者)。

我目前尝试使用 [typeaheadSingleWords]=true 和 typeaheadWordDelimiters=","。但这只会返回“1110009999”。

我当前的输入框是:

 <input type="text" class="form-control" 
   [(ngModel)]="autoCompleteModel" 
   [typeahead]="autoCompleteList" 
   [typeaheadItemTemplate]="filterConfig.itemTemplate"
   typeaheadOptionField="label" 
   [typeaheadWaitMs]="200" 
   [typeaheadScrollable]="true" 
   [typeaheadOptionsInScrollableView]="5" 
   [typeaheadSingleWords]=true 
   typeaheadWordDelimiters=","                         
   (typeaheadOnSelect)="onSelectAutoComplete($event)" 
   placeholder="{{filterConfig?.autoComplete?.placeholder || 'Search'}}" 
   *ngIf="showSelect" autofocus>

有没有办法使用 ngx-bootstrap>typeahead 插件来实现这一点?请问各位大佬有没有其他支持这个功能的插件?任何帮助将不胜感激。

最佳答案

有点晚了,但我目前正在使用 typeahead,我发现了这个:

  <input
    [formControl]="optionValue"
    typeaheadGroupField="type"
    [typeahead]="allOptions"
    [typeaheadItemTemplate]="myCustomItemTemplate"
    [optionsListTemplate]="myCustomListTemplate"
    placeholder=""
  />

  <ng-template #myCustomListTemplate let-matches="matches" let-itemTemplate="itemTemplate" let-query="query">
    <ng-template ngFor let-match let-i="index" [ngForOf]="matches">
      <ng-template
        [ngTemplateOutlet]="itemTemplate"
        [ngTemplateOutletContext]="{ item: match.item, index: i, match: match, query: query }"
      ></ng-template>
    </ng-template>
  </ng-template>

  <ng-template #myCustomItemTemplate let-model="item" let-index="index">
    <span> Model {{ index }}: {{ model | json }} </span>
  </ng-template>

这将允许您自定义列表和元素,以便您可以根据需要显示它。

关于Angular - 自定义 ngx-bootstrap Typeahead 以根据以逗号分隔的多个值自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56078610/

相关文章:

angular - 如何以 Angular 动态删除组件

javascript - Bloodhound typeahead 在控制台 laravel 中未定义

angular charts.js 圆环图动态设置中心文本

unit-testing - Jasmine 单元测试抽象类

angular - 刷新浏览器 Angular 应用程序不工作。当我将应用程序部署到 apache tomcat 时出现 404 错误

unit-testing - 错误 : Expected one matching request for criteria "Match by function: ", 未找到

android - deviceready 事件未在 Angular 混合应用程序中触发

jquery - 在 typeahead.js 中使用预取而不是本地时出现问题

jquery - 在单个页面中使用多个 Bootstrap Typeahead 字段?

javascript - 如何正确寻址响应数据?