-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuscador.js
54 lines (45 loc) · 1.41 KB
/
buscador.js
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
'use strict';
class Search {
constructor(obj) {
this.el = document.querySelector(obj.el);
this.list = obj.list;
this.init();
}
init() {
let input, listContainer;
this.el.innerHTML = `
<input placeholder="Buscar aqui" type="text" class="input"/>
<listgroup class="is-visible" id="searchList"></listgroup>
`;
input = document.querySelector('.input');
listContainer = document.querySelector('#searchList');
this.watch(input, this.list, listContainer);
}
watch(input, list, search) {
input.addEventListener('keyup', () => {
search.innerHTML = '';
let value = input.value.toLowerCase(),
listT = list.length,
existe = 0;
for (let i = 0; i < listT; i++) {
let text = list[i].toLowerCase();
if (value != '') {
existe = ~text.indexOf(value);
if (existe != 0) {
this.updateList(search, list[i]);
}
}
}
}, false);
}
updateList(el, text) {
el.innerHTML += `<list-item>${text}</list-item>`;
}
add(item) {
this.list.push(item);
}
remove(item) {
let position = this.list.indexOf(item);
this.list.splice(position, 1);
}
}