为angular2实现自动完成

前端之家收集整理的这篇文章主要介绍了为angular2实现自动完成前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法找到一个良好的自动完成组件Angular2。只是任何东西,我可以传递一个键的列表,标签对象,并在输入字段有一个很好的自动完成。
Kendo不支持angular2,它是我们主要在内部使用。角材料似乎也不支持角2。
任何人都可以告诉我正确的方向或让我知道他们在使用什么?

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

  1. import {Component,EventEmitter,Input,Output} from 'angular2/core';
  2. import {Control} from 'angular2/common';
  3. import {Observable} from 'rxjs/Observable';
  4. import {SimpleKeyValue} from '../models/simple-key-value'
  5. import 'rxjs/add/operator/map';
  6. import 'rxjs/add/operator/debounceTime';
  7. import 'rxjs/add/operator/distinctUntilChanged';
  8.  
  9. @Component({
  10. selector: 'general-typeahead',template: ` <div>
  11. <div class="input-group">
  12. <input type="text" [ngFormControl] = "term" class="form-control" placeholder={{placeHolder}} >
  13. </div>
  14. <ul>
  15. <li class="item" *ngFor="#item of matchingItems" (click)="selectItem(item)">
  16. {{item.value}}
  17. </li>
  18. </ul>
  19. </div>`
  20. })
  21.  
  22. export class GeneralTypeahead {
  23.  
  24. matchingItems: Array<SimpleKeyValue>;
  25. term = new Control();
  26.  
  27. @Input() allItems: Array<SimpleKeyValue>;
  28. @Input() placeHolder: string;
  29. @Output() onSelectItem = new EventEmitter<SimpleKeyValue>();
  30.  
  31. constructor() {
  32. this.term.valueChanges
  33. .distinctUntilChanged()
  34. .debounceTime(200)
  35. .subscribe((term : string) => this.matchingItems = this.allItems.filter(sl => sl.value.toLowerCase().indexOf(term.toLowerCase()) > -1));
  36. }
  37.  
  38. selectItem(sl: SimpleKeyValue) {
  39. this.onSelectItem.emit(sl);
  40. }
  41. }
更新:这个答案导致了 ng2-completer的Angular2自动完成组件的开发
这是Angular2的退出自动完成组件的列表:

> ng2-completer
> ng2-auto-complete
> ng2-typeahead

信用到@ dan-cancro与这个想法相符

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

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

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

它看起来对我来说,第二种方式是一个更好的选择,因为它使用角2核心机制,而不是通过直接与DOM工作绕过他们,因此我会使用这种方法

这是指令代码

  1. "use strict";
  2. import {Directive,DynamicComponentLoader,ComponentRef,Output,OnInit,ViewContainerRef} from "@angular/core";
  3. import {Promise} from "es6-promise";
  4. import {AutocompleteList} from "./autocomplete-list";
  5.  
  6. @Directive({
  7. selector: "[ng2-autocomplete]",// The attribute for the template that uses this directive
  8. host: {
  9. "(keyup)": "onKey($event)" // Liten to keyup events on the host component
  10. }
  11. })
  12. export class AutocompleteDirective implements OnInit {
  13. // The search function should be passed as an input
  14. @Input("ng2-autocomplete") public search: (term: string) => Promise<Array<{ text: string,data: any }>>;
  15. // The directive emits ng2AutocompleteOnSelect event when an item from the list is selected
  16. @Output("ng2AutocompleteOnSelect") public selected = new EventEmitter();
  17.  
  18. private term = "";
  19. private listCmp: ComponentRef<AutocompleteList> = undefined;
  20. private refreshTimer: any = undefined;
  21. private searchInProgress = false;
  22. private searchrequired = false;
  23.  
  24. constructor( private viewRef: ViewContainerRef,private dcl: DynamicComponentLoader) { }
  25. /**
  26. * On key event is triggered when a key is released on the host component
  27. * the event starts a timer to prevent concurrent requests
  28. */
  29. public onKey(event: any) {
  30. if (!this.refreshTimer) {
  31. this.refreshTimer = setTimeout(
  32. () => {
  33. if (!this.searchInProgress) {
  34. this.doSearch();
  35. } else {
  36. // If a request is in progress mark that a new search is required
  37. this.searchrequired = true;
  38. }
  39. },200);
  40. }
  41. this.term = event.target.value;
  42. if (this.term === "" && this.listCmp) {
  43. // clean the list if the search term is empty
  44. this.removeList();
  45. }
  46. }
  47.  
  48. public ngOnInit() {
  49. // When an item is selected remove the list
  50. this.selected.subscribe(() => {
  51. this.removeList();
  52. });
  53. }
  54.  
  55. /**
  56. * Call the search function and handle the results
  57. */
  58. private doSearch() {
  59. this.refreshTimer = undefined;
  60. // if we have a search function and a valid search term call the search
  61. if (this.search && this.term !== "") {
  62. this.searchInProgress = true;
  63. this.search(this.term)
  64. .then((res) => {
  65. this.searchInProgress = false;
  66. // if the term has changed during our search do another search
  67. if (this.searchrequired) {
  68. this.searchrequired = false;
  69. this.doSearch();
  70. } else {
  71. // display the list of results
  72. this.displayList(res);
  73. }
  74. })
  75. .catch(err => {
  76. console.log("search error:",err);
  77. this.removeList();
  78. });
  79. }
  80. }
  81.  
  82. /**
  83. * Display the list of results
  84. * Dynamically load the list component if it doesn't exist yet and update the suggestions list
  85. */
  86. private displayList(list: Array<{ text: string,data: any }>) {
  87. if (!this.listCmp) {
  88. this.dcl.loadNextToLocation(AutocompleteList,this.viewRef)
  89. .then(cmp => {
  90. // The component is loaded
  91. this.listCmp = cmp;
  92. this.updateList(list);
  93. // Emit the selectd event when the component fires its selected event
  94. (<AutocompleteList>(this.listCmp.instance)).selected
  95. .subscribe(selectedItem => {
  96.  
  97. this.selected.emit(selectedItem);
  98. });
  99. });
  100. } else {
  101. this.updateList(list);
  102. }
  103. }
  104.  
  105. /**
  106. * Update the suggestions list in the list component
  107. */
  108. private updateList(list: Array<{ text: string,data: any }>) {
  109. if (this.listCmp) {
  110. (<AutocompleteList>(this.listCmp.instance)).list = list;
  111. }
  112. }
  113.  
  114. /**
  115. * remove the list component
  116. */
  117. private removeList() {
  118. this.searchInProgress = false;
  119. this.searchrequired = false;
  120. if (this.listCmp) {
  121. this.listCmp.destroy();
  122. this.listCmp = undefined;
  123. }
  124. }
  125. }

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

  1. "use strict";
  2. import {Component,EventEmitter} from "@angular/core";
  3.  
  4. @Component({
  5. selector: "autocomplete-list",template: `<div class="dropdown-menu search-results">
  6. <a *ngFor="let item of list" class="dropdown-item" (click)="onClick(item)">{{item.text}}</a>
  7. </div>`,// Use a bootstrap 4 dropdown-menu to display the list
  8. styles: [".search-results { position: relative; right: 0; display: block; padding: 0; overflow: hidden; font-size: .9rem;}"]
  9. })
  10. export class AutocompleteList {
  11. // Emit a selected event when an item in the list is selected
  12. @Output() public selected = new EventEmitter();
  13.  
  14. public list;
  15.  
  16. /**
  17. * Listen for a click event on the list
  18. */
  19. public onClick(item: {text: string,data: any}) {
  20. this.selected.emit(item);
  21. }
  22. }

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

  1. "use strict";
  2. import {Component} from "@angular/core";
  3.  
  4. import {AutocompleteDirective} from "../component/ng2-autocomplete/autocomplete";
  5.  
  6. @Component({
  7. selector: "my-cmp",directives: [AutocompleteDirective],template: `<input class="form-control" type="text" [ng2-autocomplete]="search()" (ng2AutocompleteOnSelect)="onItemSelected($event)" autocomplete="off">`
  8. })
  9. export class MyComponent {
  10.  
  11. /**
  12. * generate a search function that returns a Promise that resolves to array of text and optionally additional data
  13. */
  14. public search() {
  15. return (filter: string): Promise<Array<{ text: string,data: any }>> => {
  16. // do the search
  17. resolve({text: "one item",data: null});
  18. };
  19. }
  20.  
  21. /**
  22. * handle item selection
  23. */
  24. public onItemSelected(selected: { text: string,data: any }) {
  25. console.log("selected: ",selected.text);
  26. }
  27. }

更新:与angular2 rc.1兼容的代码

猜你在找的Angularjs相关文章