mirror of
https://github.com/Alvin-Zilverstand/pokedex.git
synced 2026-03-06 11:07:31 +01:00
Add sorting functionality for Pokémon list and format Pokémon ID display
This commit is contained in:
@@ -4,12 +4,14 @@
|
|||||||
const listWrapper = document.querySelector(".list-wrapper");
|
const listWrapper = document.querySelector(".list-wrapper");
|
||||||
const searchInput = document.querySelector("#search-input");
|
const searchInput = document.querySelector("#search-input");
|
||||||
const notFoundMessage = document.querySelector("#not-found-message");
|
const notFoundMessage = document.querySelector("#not-found-message");
|
||||||
|
const sortWrapper = document.querySelector(".sort-wrapper");
|
||||||
|
|
||||||
let allPokemons = [];
|
let allPokemons = [];
|
||||||
let filteredPokemons = [];
|
let filteredPokemons = [];
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
fetchPokemons();
|
fetchPokemons();
|
||||||
|
setupSorting();
|
||||||
});
|
});
|
||||||
|
|
||||||
function fetchPokemons() {
|
function fetchPokemons() {
|
||||||
@@ -50,7 +52,7 @@
|
|||||||
listItem.className = "list-item";
|
listItem.className = "list-item";
|
||||||
listItem.innerHTML = `
|
listItem.innerHTML = `
|
||||||
<div class="number-wrap">
|
<div class="number-wrap">
|
||||||
<p class="caption-fonts">#${pokemon.id}</p>
|
<p class="caption-fonts">#${String(pokemon.id).padStart(3, "0")}</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="img-wrap">
|
<div class="img-wrap">
|
||||||
<img src="${pokemon.image_url}" alt="${pokemon.name}" />
|
<img src="${pokemon.image_url}" alt="${pokemon.name}" />
|
||||||
@@ -69,4 +71,34 @@
|
|||||||
notFoundMessage.style.display = "none";
|
notFoundMessage.style.display = "none";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setupSorting() {
|
||||||
|
const sortOptions = document.querySelectorAll(".filter-wrap input[name='filters']");
|
||||||
|
sortOptions.forEach((option) => {
|
||||||
|
option.addEventListener("change", handleSortChange);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleSortChange(event) {
|
||||||
|
const sortValue = event.target.value;
|
||||||
|
|
||||||
|
switch (sortValue) {
|
||||||
|
case "number-asc":
|
||||||
|
filteredPokemons.sort((a, b) => a.id - b.id);
|
||||||
|
break;
|
||||||
|
case "number-desc":
|
||||||
|
filteredPokemons.sort((a, b) => b.id - a.id);
|
||||||
|
break;
|
||||||
|
case "name-asc":
|
||||||
|
filteredPokemons.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
|
break;
|
||||||
|
case "name-desc":
|
||||||
|
filteredPokemons.sort((a, b) => b.name.localeCompare(a.name));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.warn("Unknown sort option:", sortValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
displayPokemons(filteredPokemons);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user