-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (75 loc) · 3.02 KB
/
script.js
File metadata and controls
92 lines (75 loc) · 3.02 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
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
const divResult = document.getElementById("result");
const searchInput = document.getElementById("searchInput");
const paginationContainer = document.createElement("div");
paginationContainer.className = "pagination";
const prevButton = document.createElement("button");
prevButton.innerText = "Anterior";
prevButton.disabled = true;
const nextButton = document.createElement("button");
nextButton.innerText = "Próximo";
paginationContainer.appendChild(prevButton);
paginationContainer.appendChild(nextButton);
document.body.appendChild(paginationContainer);
let currentPage = 1;
const itemsPerPage = 10;
let allCharacters = [];
async function loadAllCharacters() {
allCharacters = [];
for (let charactersID = 1; charactersID <= 58; charactersID++) {
const url = `https://dragonball-api.com/api/characters/${charactersID}`;
try {
const response = await fetch(url);
if (!response.ok) continue;
const data = await response.json();
allCharacters.push(data);
} catch (error) {
console.error(`Erro ao buscar personagem ${charactersID}:`, error);
}
}
}
function filterAndDisplayCharacters(query = '') {
divResult.innerHTML = '';
const filteredCharacters = allCharacters.filter(character =>
character.name.toLowerCase().startsWith(query.toLowerCase())
);
const startIndex = (currentPage - 1) * itemsPerPage;
const endIndex = startIndex + itemsPerPage;
const charactersToDisplay = filteredCharacters.slice(startIndex, endIndex);
if (charactersToDisplay.length > 0) {
charactersToDisplay.forEach(character => {
const card = document.createElement("div");
card.className = "card";
card.innerHTML = `
<h2>${character.name}</h2>
<img src="${character.image}" alt="${character.name}" style="width: 150px; height: auto; border-radius: 8px;"/>
<p><strong>Ki:</strong> ${character.ki}</p>
<p><strong>MaxKi:</strong> ${character.maxKi}</p>
<p><strong>Race:</strong> ${character.race}</p>
<p><strong>Gender:</strong> ${character.gender}</p>
<p><strong>Affiliation:</strong> ${character.affiliation}</p>
`;
divResult.appendChild(card);
});
} else {
divResult.innerHTML = `<p>Nenhum personagem encontrado para "<strong>${query}</strong>".</p>`;
}
prevButton.disabled = currentPage === 1;
nextButton.disabled = endIndex >= filteredCharacters.length;
}
searchInput.addEventListener('input', () => {
currentPage = 1;
filterAndDisplayCharacters(searchInput.value.trim());
});
prevButton.addEventListener("click", () => {
if (currentPage > 1) {
currentPage--;
filterAndDisplayCharacters(searchInput.value.trim());
}
});
nextButton.addEventListener("click", () => {
currentPage++;
filterAndDisplayCharacters(searchInput.value.trim());
});
loadAllCharacters().then(() => {
filterAndDisplayCharacters();
});