-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (58 loc) · 2.67 KB
/
script.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
"use strict";
document.addEventListener("DOMContentLoaded", event => {
const search = document.querySelector("#search");
const matchList = document.querySelector("#matchList");
const matchList2 = document.querySelector("#matchList2");
//Cherche suggestion.php et le filtre
const searchArbres = async searchText => {
const response = await fetch("./assets/php/suggestion.php");
const arbres = await response.json();
// Vérifie si le texte saisi correspond à un arbre
let matches = arbres.filter(arbre => {
// première proposition (le ^ désigne le premier caractère)
const regex = new RegExp(`^${searchText}`, 'gi');
// Le premier argument de la fonction RegExp() est le motif de l'expression régulière utilisé pour rechercher des correspondances. Le second argument représente les options de l'expression régulière (ici 'gi' pour global et ignorer la casse).
return arbre.vernaculaire.match(regex)
});
// seconde proposition
let matches2 = arbres.filter(arbre => {
const regex = new RegExp(`${searchText}`, 'gi');
return arbre.vernaculaire.match(regex)
});
// Si le texte saisi est vide, on ne propose rien
if (searchText.length === 0) {
matches = [];
matchList.innerHTML = '';
}
// Génère le html pour chaque valeur
outputHtml(matches);
outputHtml2(matches2);
};
// Renvoie le html pour chaque valeur
const outputHtml = matches => {
// Si il y a des résultats, on les affiche
if (matches.length > 0) {
// Réduit le nombre de résultats
const html = matches.slice(0, 3).map(match => `
<li class="card card-body bg-dark mb-1">
<a class="text-decoration-none link-light" href="element.php?search=${match.vernaculaire}">${match.vernaculaire} ${match.latin}</a>
</li>
`
).join('');
matchList.innerHTML = html;
}
}
const outputHtml2 = matches => {
if (matches.length > 0) {
const html = matches.slice(0, 3).map(match => `
<li class="card card-body text-white bg-secondary mb-1">
<a class="text-decoration-none link-light" href="recherche.php?search=${match.vernaculaire}">${match.vernaculaire} (${match.famille})</a>
</li>
`
).join('');
matchList2.innerHTML = html;
}
}
// Ecoute l'évènement keyup
search.addEventListener("keyup", () => searchArbres(search.value))
});