【问题标题】:Implementing autocomplete实现自动完成
【发布时间】:2016-06-23 06:01:04
【问题描述】:

我很难为 Angular2 找到一个好的自动完成组件。只是我可以将键标签对象列表传递给并在 input 字段上具有很好的自动完成功能的任何内容。

Kendo 还不支持 Angular 2,而且我们内部主要使用它。 Angular Material 似乎也不支持 Angular 2。

谁能指出我正确的方向或让我知道他们正在使用什么?

这是我到目前为止所构建的。这很糟糕,我想找一些看起来不错的东西。

import {Component, EventEmitter, Input, Output} from 'angular2/core';
import {Control} from 'angular2/common';
import {Observable} from 'rxjs/Observable';
import {SimpleKeyValue} from '../models/simple-key-value'
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';

@Component({
selector: 'general-typeahead',
template: ` <div>
            <div class="input-group">
            <input type="text" [ngFormControl] = "term" class="form-control" placeholder={{placeHolder}} >
            </div>
            <ul>
                <li class="item" *ngFor="#item of matchingItems" (click)="selectItem(item)">
                    {{item.value}}
                </li>
            </ul>              
</div>`
})

export class GeneralTypeahead {

  matchingItems: Array<SimpleKeyValue>;
  term = new Control();

  @Input() allItems: Array<SimpleKeyValue>;
  @Input() placeHolder: string;
  @Output() onSelectItem = new EventEmitter<SimpleKeyValue>();

  constructor() {
    this.term.valueChanges
        .distinctUntilChanged()
        .debounceTime(200)
        .subscribe((term : string) => this.matchingItems = this.allItems.filter(sl => sl.value.toLowerCase().indexOf(term.toLowerCase()) > -1));
  }

  selectItem(sl: SimpleKeyValue) {
    this.onSelectItem.emit(sl);
  }
}

【问题讨论】:

  • 在 angular2 中使用 Jquery 可以吗?
  • 除非没有更好的办法,否则我宁愿不要这样做
  • Angular Material 或 Kendo UI 现在有 Angular 的预输入

标签: angular angular2-template angular2-directives


【解决方案1】:

更新:这个答案导致ng2-completer Angular2 自动完成组件的开发。 这是 Angular2 的现有自动完成组件列表:

  1. ng2-completer
  2. ng2-auto-complete
  3. ng2-typeahead

感谢@dan-cancro 提出这个想法

为希望创建自己的指令的人保留原始答案:

要显示自动完成列表,我们首先需要一个attribute directive,它将根据输入文本返回建议列表,然后将它们显示在下拉列表中。 该指令有 2 个选项来显示列表:

  1. 获取对 nativeElement 的引用并直接操作 DOM
  2. 使用 DynamicComponentLoader 动态加载列表组件

在我看来,第二种方式是更好的选择,因为它使用 Angular 2 核心机制,而不是通过直接使用 DOM 来绕过它们,因此我将使用这种方法。

这是指令代码:

"use strict";
import {Directive, DynamicComponentLoader, Input, ComponentRef, Output, EventEmitter, OnInit, ViewContainerRef} from "@angular/core";
import {Promise} from "es6-promise";
import {AutocompleteList} from "./autocomplete-list";

@Directive({
    selector: "[ng2-autocomplete]", // The attribute for the template that uses this directive
    host: {
        "(keyup)": "onKey($event)" // Liten to keyup events on the host component
    }
})
export class AutocompleteDirective implements OnInit {
    // The search function should be passed as an input
    @Input("ng2-autocomplete") public search: (term: string) => Promise<Array<{ text: string, data: any }>>;
    // The directive emits ng2AutocompleteOnSelect event when an item from the list is selected
    @Output("ng2AutocompleteOnSelect") public selected = new EventEmitter();

    private term = "";
    private listCmp: ComponentRef<AutocompleteList> = undefined;
    private refreshTimer: any = undefined;
    private searchInProgress = false;
    private searchRequired = false;

    constructor( private viewRef: ViewContainerRef, private dcl: DynamicComponentLoader) { }
    /**
     * On key event is triggered when a key is released on the host component
     * the event starts a timer to prevent concurrent requests
     */
    public onKey(event: any) {
        if (!this.refreshTimer) {
            this.refreshTimer = setTimeout(
            () => {
                if (!this.searchInProgress) {
                    this.doSearch();
                } else {
                    // If a request is in progress mark that a new search is required
                    this.searchRequired = true;
                }
            },
            200);
        }
        this.term = event.target.value;
        if (this.term === "" && this.listCmp) {
            // clean the list if the search term is empty
            this.removeList();
        }
    }

    public ngOnInit() {
        // When an item is selected remove the list
        this.selected.subscribe(() => {
            this.removeList();
        });
    }

    /**
     * Call the search function and handle the results
     */
    private doSearch() {
        this.refreshTimer = undefined;
        // if we have a search function and a valid search term call the search
        if (this.search && this.term !== "") {
            this.searchInProgress = true;
            this.search(this.term)
            .then((res) => {
                this.searchInProgress = false;
                // if the term has changed during our search do another search
                if (this.searchRequired) {
                    this.searchRequired = false;
                    this.doSearch();
                } else {
                    // display the list of results
                    this.displayList(res);
                }
            })
            .catch(err => {
                console.log("search error:", err);
                this.removeList();
            });
        }
    }

    /**
     * Display the list of results
     * Dynamically load the list component if it doesn't exist yet and update the suggestions list
     */
    private displayList(list: Array<{ text: string, data: any }>) {
        if (!this.listCmp) {
            this.dcl.loadNextToLocation(AutocompleteList, this.viewRef)
            .then(cmp => {
                // The component is loaded
                this.listCmp = cmp;
                this.updateList(list);
                // Emit the selectd event when the component fires its selected event
                (<AutocompleteList>(this.listCmp.instance)).selected
                    .subscribe(selectedItem => {

                    this.selected.emit(selectedItem);
                });
            });
        } else {
            this.updateList(list);
        }
    }

    /**
     * Update the suggestions list in the list component
     */
    private updateList(list: Array<{ text: string, data: any }>) {
        if (this.listCmp) {
            (<AutocompleteList>(this.listCmp.instance)).list = list;
        }
    }

    /**
     * remove the list component
     */
    private removeList() {
        this.searchInProgress = false;
        this.searchRequired = false;
        if (this.listCmp) {
            this.listCmp.destroy();
            this.listCmp = undefined;
        }
    }
}

该指令动态加载下拉组件,这是使用引导程序 4 的此类组件的示例:

"use strict";
import {Component, Output, EventEmitter} from "@angular/core";

@Component({
    selector: "autocomplete-list",
    template: `<div class="dropdown-menu  search-results">
                    <a *ngFor="let item of list" class="dropdown-item" (click)="onClick(item)">{{item.text}}</a>
               </div>`, // Use a bootstrap 4 dropdown-menu to display the list
    styles: [".search-results { position: relative; right: 0; display: block; padding: 0; overflow: hidden; font-size: .9rem;}"]
})
export class AutocompleteList  {
    // Emit a selected event when an item in the list is selected
    @Output() public selected = new EventEmitter();

    public list;

    /**
     * Listen for a click event on the list
     */
    public onClick(item: {text: string, data: any}) {
        this.selected.emit(item);
    }
}

要在另一个组件中使用该指令,您需要导入该指令,将其包含在组件指令中并为其提供搜索功能和事件处理程序以供选择:

 "use strict";
import {Component} from "@angular/core";

import {AutocompleteDirective} from "../component/ng2-autocomplete/autocomplete";

@Component({
    selector: "my-cmp",
    directives: [AutocompleteDirective],
    template: `<input class="form-control" type="text" [ng2-autocomplete]="search()" (ng2AutocompleteOnSelect)="onItemSelected($event)" autocomplete="off">`
})
export class MyComponent  {

    /**
     * generate a search function that returns a Promise that resolves to array of text and optionally additional data
     */  
    public search() {
        return (filter: string): Promise<Array<{ text: string, data: any }>> => {
            // do the search
            resolve({text: "one item", data: null});
        };
    }

    /**
     * handle item selection
     */  
    public onItemSelected(selected: { text: string, data: any }) {
        console.log("selected: ", selected.text);
    }
}

更新:代码兼容 angular2 rc.1

【讨论】:

  • 当您决定制作 ng2-autocomplete 时,您是否考虑过将 angucomplete-alt 更新到 Angular 2? github.com/ghiden/angucomplete-alt你认为这需要做多少工作?
  • @DanCancro 我没有考虑过这一点,因为它计划是一个示例代码而不是一个库。合并这两个项目应该不会花费很长时间,因为 angucomlete-alt 只是一个组件。它可能会通过从 angucomlete-alt 获取逻辑和 css 并将它们放在这个项目中来工作。
  • @OferHerman,有没有办法绑定列表项的其他属性而不是文本?我的意思是有一个 id 和文本值列表,用户输入文本以选择一个项目,然后使用 [NgModel] 绑定 id
  • @mehran 你可以创建一个新的输入,比如@Input() searchPropertyName = "text" 并且在任何使用item.text 的地方使用item[searchPropertyName]
  • ng2-typeahead 现在已被弃用,因为它的官方 git hub repo github.com/brinkmanjg/ng2-typeahead
【解决方案2】:

PrimeNG 具有原生 AutoComplete 组件,具有模板和多选等高级功能。

http://www.primefaces.org/primeng/#/autocomplete

【讨论】:

  • 没有获得自动完成的选定值?你有同样的工作例子吗?
【解决方案3】:

我认为你可以使用typeahead.js。它有打字稿定义。所以我想如果你使用打字稿进行开发,它会很容易使用。

【讨论】:

  • 谢谢,但我想避免需要 jquery。理想情况下,它会使用 RxJs 和 observables。我创建了一些基本的东西,但我希望如果有人已经做得很好,我可以省去重新发明轮子
  • 我找不到任何使用 RxJs 而不是 Jquery 的自动完成功能。但是我发现了 autocomplete 的这个 RxJs 示例,它使用 jquery 和旧版本的引导程序。您可以尝试从这个example 获取整体逻辑。它可能会为您节省一些时间。
【解决方案4】:

我知道你已经有了几个答案,但我遇到了类似的情况,我的团队不想依赖繁重的库或任何与引导程序相关的东西,因为我们使用的是材料,所以我使用了自己的自动完成控件类似材料的样式,你可以使用我的autocomplete 或者至少你可以给你一些指导,关于如何上传你的组件以在 NPM 上共享的简单示例的文档并不多。

【讨论】:

    【解决方案5】:

    我为 anuglar2 自动完成创建了一个模块 在此模块中,您可以使用数组或 url npm 链接:ang2-autocomplete

    【讨论】:

      【解决方案6】:

      我已经根据这个答案/其他关于这个主题和其他主题的教程中的一些想法构建了一个相当简单、可重用且功能强大的 Angular2 自动完成组件。它绝不是全面的,但如果您决定自己构建它可能会有所帮助。

      组件:

      import { Component, Input, Output, OnInit, ContentChild, EventEmitter, HostListener } from '@angular/core';
      import { Observable } from "rxjs/Observable";
      import { AutoCompleteRefDirective } from "./autocomplete.directive";
      
      @Component({
          selector: 'autocomplete',
          template: `
      <ng-content></ng-content>
      <div class="autocomplete-wrapper" (click)="clickedInside($event)">
          <div class="list-group autocomplete" *ngIf="results">
              <a [routerLink]="" class="list-group-item" (click)="selectResult(result)" *ngFor="let result of results; let i = index" [innerHTML]="dataMapping(result) | highlight: query" [ngClass]="{'active': i == selectedIndex}"></a>
          </div>
      </div>
          `,
          styleUrls: ['./autocomplete.component.css']
      })
      export class AutoCompleteComponent implements OnInit {
      
          @ContentChild(AutoCompleteRefDirective)
          public input: AutoCompleteRefDirective;
      
          @Input() data: (searchTerm: string) => Observable<any[]>;
          @Input() dataMapping: (obj: any) => string;
          @Output() onChange = new EventEmitter<any>();
      
          @HostListener('document:click', ['$event'])
          clickedOutside($event: any): void {
              this.clearResults();
          }
      
          public results: any[];
          public query: string;
          public selectedIndex: number = 0;
          private searchCounter: number = 0;
      
          ngOnInit(): void {
              this.input.change
                  .subscribe((query: string) => {
                      this.query = query;
                      this.onChange.emit();
                      this.searchCounter++;
                      let counter = this.searchCounter;
      
                      if (query) {
                          this.data(query)
                              .subscribe(data => {
                                  if (counter == this.searchCounter) {
                                      this.results = data;
                                      this.input.hasResults = data.length > 0;
                                      this.selectedIndex = 0;
                                  }
                              });
                      }
                      else this.clearResults();
                  });
      
              this.input.cancel
                  .subscribe(() => {
                      this.clearResults();
                  });
      
              this.input.select
                  .subscribe(() => {
                      if (this.results && this.results.length > 0)
                      {
                          this.selectResult(this.results[this.selectedIndex]);
                      }
                  });
      
              this.input.up
                  .subscribe(() => {
                      if (this.results && this.selectedIndex > 0) this.selectedIndex--;
                  });
      
              this.input.down
                  .subscribe(() => {
                      if (this.results && this.selectedIndex + 1 < this.results.length) this.selectedIndex++;
                  });
          }
      
          selectResult(result: any): void {
              this.onChange.emit(result);
              this.clearResults();
          }
      
          clickedInside($event: any): void {
              $event.preventDefault();
              $event.stopPropagation();
          }
      
          private clearResults(): void {
              this.results = [];
              this.selectedIndex = 0;
              this.searchCounter = 0;
              this.input.hasResults = false;
          }
      }
      

      组件 CSS:

      .autocomplete-wrapper {
          position: relative;
      }
      
      .autocomplete {
          position: absolute;
          z-index: 100;
          width: 100%;
      }
      

      指令:

      import { Directive, Input, Output, HostListener, EventEmitter } from '@angular/core';
      
      @Directive({
          selector: '[autocompleteRef]'
      })
      export class AutoCompleteRefDirective {
          @Input() hasResults: boolean = false;
          @Output() change = new EventEmitter<string>();
          @Output() cancel = new EventEmitter();
          @Output() select = new EventEmitter();
          @Output() up = new EventEmitter();
          @Output() down = new EventEmitter();
      
          @HostListener('input', ['$event'])
          oninput(event: any) {
              this.change.emit(event.target.value);
          }
      
          @HostListener('keydown', ['$event'])
          onkeydown(event: any)
          {
              switch (event.keyCode) {
                  case 27:
                      this.cancel.emit();
                      return false;
                  case 13:
                      var hasResults = this.hasResults;
                      this.select.emit();
                      return !hasResults;
                  case 38:
                      this.up.emit();
                      return false;
                  case 40:
                      this.down.emit();
                      return false;
                  default:
              }
          }
      }
      

      高亮管道:

      import { Pipe, PipeTransform } from '@angular/core';
      
      @Pipe({
          name: 'highlight'
      })
      
      export class HighlightPipe implements PipeTransform {
          transform(value: string, args: any): any {
              var re = new RegExp(args, 'gi');
      
              return value.replace(re, function (match) {
                  return "<strong>" + match + "</strong>";
              })
      
          }
      }
      

      实现:

      import { Component } from '@angular/core';
      import { Observable } from "rxjs/Observable";
      import { Subscriber } from "rxjs/Subscriber";
      
      @Component({
          selector: 'home',
          template: `
      <autocomplete [data]="getData" [dataMapping]="dataMapping" (onChange)="change($event)">
          <input type="text" class="form-control" name="AutoComplete" placeholder="Search..." autocomplete="off" autocompleteRef />
      </autocomplete>
          `
      })
      export class HomeComponent {
      
          getData = (query: string) => this.search(query);
      
          // The dataMapping property controls the mapping of an object returned via getData.
          // to a string that can be displayed to the use as an option to select.
          dataMapping = (obj: any) => obj;
      
          // This function is called any time a change is made in the autocomplete.
          // When the text is changed manually, no object is passed.
          // When a selection is made the object is passed.
          change(obj: any): void {
              if (obj) {
                  // You can do pretty much anything here as the entire object is passed if it's been selected.
                  // Navigate to another page, update a model etc.
                  alert(obj);
              }
          }
      
          private searchData = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
      
          // This function mimics an Observable http service call.
          // In reality it's probably calling your API, but today it's looking at mock static data.
          private search(query: string): Observable<any>
          {
              return new Observable<any>((subscriber: Subscriber<any>) => subscriber
                  .next())
                  .map(o => this.searchData.filter(d => d.indexOf(query) > -1));
          }
      }
      

      【讨论】:

      • 请添加到 stackblitz
      【解决方案7】:

      我想补充一些还没有人提到的东西:ng2-input-autocomplete

      NPM:https://www.npmjs.com/package/ng2-input-autocomplete

      GitHub:https://github.com/liuy97/ng2-input-autocomplete#readme

      【讨论】:

        猜你喜欢
        • 2018-04-23
        • 2013-01-28
        • 2012-01-03
        • 2013-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多