-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchFilter.js
70 lines (62 loc) · 2.32 KB
/
SearchFilter.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const dropdownList = document.getElementById("myDropdown");
const originalOptions = Array.from(dropdownList.options);
// console.log(originalOptions);
const origOptArray = Array.from(dropdownList.options).map((option) => {
return option.value;
});
// console.log(origOptArray);
const filterOptions = (inputValue) => {
const updatedOptions = dropdownList.options;
const removeSelected = (options) => {
const filteredOptions = removeSelected(options);
return filteredOptions;
};
const matchingOptions = Array.prototype.filter.call(filteredOptions, (option) =>
option.textContent.toUpperCase().startsWith(inputValue.toUpperCase())
);
const nonMatchingOptions = Array.prototype.filter.call(
filteredOptions,
(option) => !option.value.toUpperCase().startsWith(inputValue.toUpperCase())
);
return { matchingOptions, nonMatchingOptions };
console.log(matchingOptions, nonMatchingOptions);
while (dropdownList.firstChild) {
dropdownList.removeChild(dropdownList.firstChild);
}
const newOptions = matchingOptions.map((option) => {
const newOption = document.createElement("option");
newOption.value = option.value;
newOption.textContent = option.textContent;
return newOption;
});
dropdownList.append(...newOptions);
// Add the new options to the dropdown list
// dropdownList.childNodes.forEach((node) => {
// // remove option if the option is already in the dropdown list
// console.log(node.textContent, inputValue);
// while (dropdownList.firstChild) {
// dropdownList.removeChild(dropdownList.firstChild);
// }
// if (node.textContent.startsWith(inputValue)) {
// dropdownList.appendChild(node);
// } else {
// // dropdownList.removeChild(node);
// }
// // Add the first option back to the dropdown list
const firstOption = document.createElement("option");
firstOption.value = "";
firstOption.textContent = "-- SELECT --";
firstOption.disabled = true;
dropdownList.append(firstOption);
};
const input = document.getElementById("myInput");
const firstOption = document.createElement("option");
firstOption.value = "";
firstOption.textContent = "-- SELECT --";
firstOption.disabled = true;
dropdownList[0].textContent = "";
dropdownList[0].append(firstOption);
const inputField = document.getElementById("myInput");
inputField.addEventListener("input", function () {
filterOptions(inputField.value);
});