1 | # Angular Autocomplete (Angular 2 +)
|
2 | See [Demo](https://gmerabishvili.github.io/angular-ng-autocomplete/) or try in [Stackblitz](https://stackblitz.com/edit/angular-ng-autocomplete)
|
3 |
|
4 | Table of contents
|
5 | =================
|
6 |
|
7 | * [Features](#features)
|
8 | * [Getting started](#getting-started)
|
9 | * [Usage](#usage)
|
10 | * [API](#api)
|
11 | * [Styles](#styles)
|
12 |
|
13 | ## Features
|
14 | - [x] Flexible autocomplete with client/server filtering.
|
15 | - [x] Variable properties and event bindings.
|
16 | - [x] Selection history.
|
17 | - [x] Custom item and 'not found' templates.
|
18 | - [x] Keyboard navigation.
|
19 | - [x] Accessibility.
|
20 |
|
21 | ## Getting started
|
22 | ### Step 1: Install `angular-ng-autocomplete`:
|
23 |
|
24 | #### NPM
|
25 | ```shell
|
26 | npm i angular-ng-autocomplete
|
27 | ```
|
28 | ### Step 2: Import the AutocompleteLibModule:
|
29 | ```js
|
30 | import {AutocompleteLibModule} from 'angular-ng-autocomplete';
|
31 |
|
32 | @NgModule({
|
33 | declarations: [AppComponent],
|
34 | imports: [AutocompleteLibModule],
|
35 | bootstrap: [AppComponent]
|
36 | })
|
37 | export class AppModule {}
|
38 | ```
|
39 | ### Usage sample
|
40 |
|
41 | ```html
|
42 | <div class="ng-autocomplete">
|
43 | <ng-autocomplete
|
44 | [data]="data"
|
45 | [searchKeyword]="keyword"
|
46 | (selected)='selectEvent($event)'
|
47 | (inputChanged)='onChangeSearch($event)'
|
48 | (inputFocused)='onFocused($event)'
|
49 | [itemTemplate]="itemTemplate"
|
50 | [notFoundTemplate]="notFoundTemplate">
|
51 | </ng-autocomplete>
|
52 |
|
53 | <ng-template #itemTemplate let-item>
|
54 | <a [innerHTML]="item"></a>
|
55 | </ng-template>
|
56 |
|
57 | <ng-template #notFoundTemplate let-notFound>
|
58 | <div [innerHTML]="notFound"></div>
|
59 | </ng-template>
|
60 | </div>
|
61 |
|
62 | ```
|
63 | ```javascript
|
64 |
|
65 | class TestComponent {
|
66 | keyword = 'name';
|
67 | data = [
|
68 | {
|
69 | id: 1,
|
70 | name: 'Usa'
|
71 | },
|
72 | {
|
73 | id: 2,
|
74 | name: 'England'
|
75 | }
|
76 | ];
|
77 |
|
78 |
|
79 | selectEvent(item) {
|
80 | // do something with selected item
|
81 | }
|
82 |
|
83 | onChangeSearch(search: string) {
|
84 | // fetch remote data from here
|
85 | // And reassign the 'data' which is binded to 'data' property.
|
86 | }
|
87 |
|
88 | onFocused(e){
|
89 | // do something when input is focused
|
90 | }
|
91 | }
|
92 | ```
|
93 |
|
94 | ## API
|
95 | ### Inputs
|
96 | | Input | Type | Default | Required | Description |
|
97 | | ------------- | ------------- | ------------- | ------------- | ------------- |
|
98 | | [data] | `Array<any>` | `null` | yes | Data of items list. It can be array of strings or array of objects. |
|
99 | | searchKeyword | `string` | `-` | yes | Variable name to filter data by. |
|
100 | | placeHolder | `string` | `-` | no | HTML `<input>` placeholder text. |
|
101 | | initialValue | `any` | `_` | no | initial/default selected value. |
|
102 | | historyIdentifier | `string` | `_` | no | History identifier of history list. When valid history identifier is given, then component stores selected item to local storage of user's browser. If it is null then history is hidden. History list is visible if at least one history item is stored. History identifier must be unique. |
|
103 | | historyHeading | `string` | `Recently selected` | no | Heading text of history list. If it is null then history heading is hidden. |
|
104 | | historyListMaxNumber | `number` | `15` | no | Maximum number of items in the history list. |
|
105 | | notFoundText | `string` | `Not found` | no | Set custom text when filter returns empty result. |
|
106 | | isLoading | `boolean` | `false` | no | Set the loading state from the parent component when data is being loaded. |
|
107 | | debounceTime | `number` | `400` | no | Delay time while typing. |
|
108 |
|
109 | ### Outputs
|
110 | | Output | Description |
|
111 | | ------------- | ------------- |
|
112 | | (selected) | Event is emitted when an item from the list is selected. |
|
113 | | (inputChanged) | Event is emitted when an input is changed. |
|
114 | | (inputFocused) | Event is emitted when an input is focused. |
|
115 | | (opened) | Event is emitted when the autocomplete panel is opened. |
|
116 | | (closed) | Event is emitted when the autocomplete panel is closed. |
|
117 |
|
118 |
|
119 | ### Methods (controls)
|
120 | Name | Description |
|
121 | | ------------- | ------------- |
|
122 | | open | Opens the autocomplete panel |
|
123 | | close | Closes the autocomplete panel |
|
124 | | focus | Focuses the autocomplete input element |
|
125 |
|
126 | To access the control methods of the component you should use `@ViewChild` decorator.
|
127 | See the example below:
|
128 |
|
129 | ```html
|
130 | <ng-autocomplete #auto></ng-autocomplete>
|
131 | ```
|
132 |
|
133 | ```javascript
|
134 | class TestComponent {
|
135 | @ViewChild('auto') auto;
|
136 |
|
137 | openPanel(e): void {
|
138 | e.stopPropagation();
|
139 | this.auto.open();
|
140 | }
|
141 |
|
142 | closePanel(e): void {
|
143 | e.stopPropagation();
|
144 | this.auto.close();
|
145 | }
|
146 |
|
147 | focus(e): void {
|
148 | e.stopPropagation();
|
149 | this.auto.focus();
|
150 | }
|
151 | }
|
152 | ```
|
153 |
|
154 | ## Styles
|
155 | If you are not happy with default styles you can easily override them:
|
156 |
|
157 | ```html
|
158 | <div class="ng-autocomplete">
|
159 | <ng-autocomplete></ng-autocomplete>
|
160 | </div>
|
161 | ```
|
162 |
|
163 | ```css
|
164 | .ng-autocomplete {
|
165 | width: 400px;
|
166 | }
|
167 | ```
|
168 |
|
169 | ### Author
|
170 | * [Giorgi Merabishvili](https://www.linkedin.com/in/giorgi-merabishvili-3719a2121/)
|
171 |
|
172 |
|