This repository was archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautocomplete.component.ts
More file actions
55 lines (46 loc) · 1.54 KB
/
autocomplete.component.ts
File metadata and controls
55 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
@Component({
selector: "bootstrap-autocomplete",
templateUrl: "./autocomplete.component.html",
styleUrls: ["./autocomplete.component.css"]
})
export class AutocompleteComponent implements OnInit {
@Input() entries: Array<any>;
@Input() entryProperty: string;
@Input() inputPlaceholder: string = "Search..."
@Input() inputId: string | number = 0;
@Output() onEntrySelected = new EventEmitter();
showResults: boolean = false;
filteredEntities: any;
filter: string;
constructor() {
}
ngOnInit() {
this.filteredEntities = this.entries;
}
filterEntries(filter: string) {
if (typeof this.entryProperty != 'undefined') {
this.filteredEntities = this.entries.filter(item => item[this.entryProperty].toLowerCase().indexOf(filter.toLowerCase()) !== -1);
} else {
this.filteredEntities = this.entries.filter(item => item.toLowerCase().indexOf(filter.toLowerCase()) !== -1);
}
}
inputFieldFocused() {
let inputId = 'inputField' + this.inputId;
let menuId = '#menu' + this.inputId + ' ' + 'a';
document.getElementById(inputId).addEventListener('keydown', function (e) {
if (e.key == "ArrowDown") {
(document.querySelectorAll(menuId)[0] as any).focus();
}
});
}
selectEntry(entry) {
if (typeof this.entryProperty != 'undefined') {
this.filter = entry[this.entryProperty];
} else {
this.filter = entry;
}
this.showResults = false;
this.onEntrySelected.emit(entry);
}
}