-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.html
159 lines (126 loc) · 4.49 KB
/
autocomplete.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html>
<head>
<title>Autocomplete</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<h1>Autocomplete</h1>
<input type="text" id="autocompleteInput" placeholder="Enter a word" />
<button id="addButton">Add Word</button>
<ul id="suggestionsList"></ul>
<!-- NOTE: This is an autocomplete example that shows how to create an autocomplete feature on an input field by using a Trie data structure. A Trie is often used for these kind of string related features such as autocomplete, spell checking etc... -->
<script>
// -------------------
// Trie implementation
class TrieNode {
constructor() {
this.children = {}
}
}
class Trie {
constructor() {
this.root = new TrieNode()
}
insert(word) {
let currentNode = this.root
for (let i = 0; i < word.length; i++) {
let char = word[i]
if (!currentNode.children[char]) {
let newNode = new TrieNode()
currentNode.children[char] = newNode
currentNode = newNode
} else {
currentNode = currentNode.children[char]
}
}
currentNode.children['*'] = true
}
search(word) {
let currentNode = this.root
for (let i = 0; i < word.length; i++) {
let char = word[i]
if (!currentNode.children[char]) return false
currentNode = currentNode.children[char]
}
return currentNode
}
collectAllWords(node = this.root, word = '', words = []) {
for (let key in node.children) {
let child = node.children[key]
if (key === '*') {
words.push(word)
} else {
this.collectAllWords(child, word + key, words)
}
}
return words
}
autoComplete(prefix) {
let currentNode = this.search(prefix)
if (!currentNode) return null
return this.collectAllWords(currentNode, prefix)
}
}
// -------------------
// Initialize the Trie
const trie = new Trie()
// Insert sample words into the Trie
trie.insert('apple')
trie.insert('banana')
trie.insert('orange')
trie.insert('grape')
trie.insert('pineapple')
// -------------------
// Get input, add button, and suggestions list elements
const input = document.getElementById('autocompleteInput')
const addButton = document.getElementById('addButton')
const suggestionsList = document.getElementById('suggestionsList')
// Display initial list of fruits
const initialSuggestions = trie.autoComplete('')
for (let i = 0; i < initialSuggestions.length; i++) {
const listItem = document.createElement('li')
listItem.textContent = initialSuggestions[i]
suggestionsList.appendChild(listItem)
}
// Event listener for input changes
input.addEventListener('input', () => {
const prefix = input.value
const suggestions = trie.autoComplete(prefix)
// Clear previous suggestions
suggestionsList.innerHTML = ''
// Display new suggestions
if (suggestions) {
for (let i = 0; i < suggestions.length; i++) {
const listItem = document.createElement('li')
listItem.textContent = suggestions[i]
suggestionsList.appendChild(listItem)
}
}
})
// Event listener for add button click
addButton.addEventListener('click', () => {
const newWord = input.value.trim()
if (newWord !== '') {
trie.insert(newWord)
input.value = ''
// Update the suggestions list
const suggestions = trie.autoComplete('')
suggestionsList.innerHTML = ''
for (let i = 0; i < suggestions.length; i++) {
const listItem = document.createElement('li')
listItem.textContent = suggestions[i]
suggestionsList.appendChild(listItem)
}
}
})
// Event listener for when the user clicks 'Enter' in the input field (add word to the list)
input.addEventListener('keyup', (event) => {
if (event.keyCode === 13) {
addButton.click() // Simulate a click on the add button
}
})
</script>
</body>
</html>