I need to filter with this method the tasks that have been completed, those that are not completed and that all appear, interacting with the three buttons; to be able to filter these tasks with these filters and that only the correct ones come out, it is the only functionality that my task list lacks.
window.addEventListener("load", () => {
let id = 0;
let text = "";
let alert = document.querySelector(".alert");
let alertGreen = document.querySelector(".alert-green");
let close = alert.firstElementChild;
let input = document.querySelector("#task");
let arrow = document.querySelector(".arrow");
let done = document.querySelectorAll(".fa-circle-check");
let trash = document.querySelectorAll(".fa-trash");
let edit = document.querySelectorAll(".fa-pencil");
let task = document.querySelectorAll(".task");
let btnAll = document.querySelector(".btn");
let btnToDo = btnAll.nextElementSibling;
let btnDone = btnToDo.nextElementSibling;
let taskArray = [].slice.call(task);
console.log(taskArray);
close.addEventListener("click", () => {
alert.classList.add("dismissible");
});
input.addEventListener("focus", () => {
document.addEventListener("keydown", (event) => {
// console.log(event.code);
if (event.code == "Enter" || event.code == "NumpadEnter") {
event.preventDefault();
}
});
});
//Alert Verde con temporizador
arrow.addEventListener("click", (event) => {
if (input.value.trim() !== "") {
alertGreen.classList.remove("dismissible");
setTimeout(() => {
alertGreen.classList.add("dismissible");
}, 5000);
}
if (input.value.trim() == "") {
//Trim elimina los espacios al inicio y al final del string
// console.log("Empty");
event.preventDefault();
input.value = "";
alert.classList.remove("class", "dismissible");
} else {
let text = input.value;
input.value = "";
id =
Number(document.querySelector("tbody")?.lastElementChild?.id) + 1 || 0;
//?? operador condicional?? por si no existe o está vacío
//Creando una nueva fila
document.querySelector("tbody").appendChild(generateRow(id, text));
if (!alert.classList.contains("dismissible")) {
alert.classList.add("dismissible");
}
}
});
done.forEach((item) => {
item.addEventListener("click", (e) => {
deleteTask(e);
});
});
trash.forEach((item) => {
item.addEventListener("click", (e) => {
removeRow(e, false);
});
});
edit.forEach((item) => {
item.addEventListener("click", (e) => {
editTask(e, false);
});
});
task.forEach((item) => {
item.addEventListener("focus", (e) => {
editTask(e, true);
});
});
});
// Funciones para refactorizar el código
const generateRow = (id, text) => {
let newRow = document.createElement("tr");
newRow.setAttribute("id", id);
newRow.innerHTML = `
<td>
<i class="fa-solid fa-circle-check fa-2x"></i>
<span class="task" contenteditable="true"> ${text} </span>
</td>
<td>
<span class="fa-stack fa-2x">
<i class="fa-solid fa-square fa-stack-2x"></i>
<i class="fa-solid fa-pencil fa-stack-1x fa-inverse"></i>
</span>
</td>
<td>
<span class="fa-stack fa-2x">
<i class="fa-solid fa-square fa-stack-2x"></i>
<i class="fa-solid fa-trash fa-stack-1x fa-inverse"></i>
</span>
</td>
`;
newRow.firstElementChild.firstElementChild.addEventListener("click", (e) => {
deleteTask(e);
});
newRow.firstElementChild.lastElementChild.addEventListener("click", (e) => {
editTask(e, true);
});
newRow.lastElementChild.firstElementChild.addEventListener("click", (e) => {
removeRow(e, false);
});
newRow.firstElementChild.nextElementSibling.firstElementChild.addEventListener(
"click",
(e) => {
editTask(e, false);
}
);
return newRow;
};
const deleteTask = (e) => {
let task = e.target.nextElementSibling;
text = task.innerHTML;
if (text.includes("<del>")) {
task.innerHTML = task.firstElementChild.textContent;
task.setAttribute("data-completed", "false");
} else {
task.innerHTML = `<del>${text}</del>`;
task.setAttribute("data-completed", "true");
}
};
const removeRow = (e, editing) => {
if (editing) {
e.target.parentNode.parentNode.remove();
} else {
e.target.parentNode.parentNode.parentNode.remove();
}
};
const editTask = (e, onFocus) => {
let editable = e;
if (onFocus) {
e.target.classList.add("editable");
document.addEventListener("keydown", (e) => {
if (e.code == "Escape") {
editable.target.classList.remove("editable");
// Elimina el puntero del foco
editable.target.blur();
if (editable.target.textContent.trim() == "") {
removeRow(editable, true);
}
}
});
editable.target.addEventListener("blur", () => {
editable.target.classList.remove("editable");
if (editable.target.textContent.trim() == "") {
removeRow(editable, true);
}
});
} else {
let editable =
e.target.parentNode.parentNode.previousElementSibling.lastElementChild;
editable.classList.add("editable");
editable.focus();
}
};
:root {
font-size: 0.875rem;
font-family: "Rubik", sans-serif;
--blue: #0d6efd;
--indigo: #6610f2;
--purple: #6f42c1;
--pink: #d63384;
--red: #dc3545;
--orange: #fd7e14;
--yellow: #ffc107;
--green: #198754;
--teal: #20c997;
--cyan: #0dcaf0;
--dark: #292a2b;
--black: #000;
--color1: #e63946;
--color2: #f1faee;
--color3: #a8dadc;
--color4: #457b9d;
--color5: #1d3557;
box-sizing: border-box;
}
*,
::before,
::after {
padding: 0;
margin: 0;
border: 0;
box-sizing: inherit;
outline: 0 none;
}
h1 {
font-size: 3.9rem;
font-family: "Smooch Sans", sans-serif;
}
h2 {
font-size: 2.25rem;
}
h3 {
font-size: 1.5rem;
}
h4 {
font-size: 1.1rem;
}
h5 {
font-size: 0.9rem;
}
h6 {
font-size: 0.7rem;
}
body {
width: 100%;
height: 100vh;
font-family: "Lato", sans-serif;
background-color: var(--color3);
background: linear-gradient(to right, var(--color2), var(--color3));
}
.nav {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.btn-group {
margin-top: 1em;
}
.btn {
background-color: #3b71ca;
color: #fff;
vertical-align: bottom;
border: 0;
font-weight: 500;
font-size: 1.3em;
text-transform: lowercase;
font-variant: small-caps;
padding: 0.625rem 1.5rem 0.5rem;
line-height: 1.5;
display: inline-block;
text-align: center;
vertical-align: middle;
cursor: pointer;
border: 0.125rem solid transparent;
padding: 0.375rem 0.75rem;
border-radius: 0.25rem;
}
.btn:hover {
transition: all 0.15s ease-in-out;
box-shadow: 0 4px 9px -4px var(--dark);
background-color: #2c68ca;
}
.btn-group>.btn:not(:last-child):not(.dropdown-toggle) {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.btn-group>.btn-group:not(:first-child)>.btn,
.btn-group>.btn:nth-child(n + 3),
.btn-group> :not(.btn-check)+.btn {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.btn-group>.btn-group:not(:first-child)>.btn,
.btn-group>.btn:nth-child(n + 3),
.btn-group> :not(.btn-check)+.btn {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.btn-group>.btn-group:not(:first-child),
.btn-group>.btn:not(:first-child) {
margin-left: -0.325rem;
}
.main {
width: 80%;
padding: 2em;
margin: 10% auto;
background-color: white;
}
.main .container {
overflow-x: auto;
}
.main form {
width: 100%;
}
.main label {
display: flex;
align-items: center;
justify-content: center;
}
.main label div {
width: 20%;
display: flex;
align-items: center;
justify-content: center;
padding: 0.5em;
background-color: var(--blue);
min-height: 60px;
font-size: 1.1rem;
border-top-right-radius: 0.3rem;
border-bottom-right-radius: 0.3rem;
transition: all 0.4s ease-in-out;
}
.main label div:hover {
background-color: #0b5ed7;
}
.main label div .fa-solid {
transition: all 0.4s ease-in-out;
}
.main label div .fa-solid:hover {
transform: translateX(10px);
}
.main input {
display: block;
width: 80%;
background-color: #bee5e3;
padding: 0.5em;
min-height: 60px;
font-size: 1.1rem;
border-top-left-radius: 0.3rem;
border-bottom-left-radius: 0.3rem;
transition: all 0.15s ease-in-out;
}
.main input::placeholder {
color: var(--dark);
}
.main input:focus {
border: 5px solid transparent;
border-radius: 0.3rem;
background-color: white;
box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
z-index: 2;
}
.main .alert {
width: 100%;
height: auto;
opacity: 1;
background-color: var(--pink);
color: white;
padding: 1em;
margin: 1em auto;
border-radius: 0.3rem;
display: flex;
align-items: flex-start;
justify-content: space-between;
transition: all 0.3s linear;
}
.main .alert .fa-square-xmark {
font-size: 1.5rem;
}
.main .alert .fa-square-xmark:hover {
color: var(--dark);
}
.main .alert-green {
width: 100%;
height: auto;
opacity: 1;
background-color: var(--green);
color: white;
padding: 1em;
margin: 1em auto;
border-radius: 0.3rem;
display: flex;
align-items: flex-start;
justify-content: space-between;
transition: all 0.3s linear;
}
.main .alert-green .fa-square-check {
font-size: 1.5rem;
}
.main .alert-green .fa-square-check:hover {
color: var(--dark);
}
.main table {
width: 100%;
border-collapse: collapse;
border-spacing: 0;
border-radius: 0.3rem;
margin: 2em auto;
}
.main tr {
background-color: #f6f6f6;
}
.main tr:nth-of-type(odd) {
background-color: #e9e9e9;
}
.main th {
background-color: #2980d9;
padding: 0.5em;
font-weight: 700;
font-size: 1.5rem;
text-transform: capitalize;
}
.main th:first-child {
border-top-left-radius: 0.3rem;
}
.main th:last-child {
border-top-right-radius: 0.3rem;
}
.main td {
font-family: "Fredoka", sans-serif;
padding: 0.8em;
}
.main td:first-child {
width: 70%;
font-size: 1.5rem;
}
.main td:nth-child(2),
.main td:nth-child(3) {
width: 15%;
font-size: 1.1rem;
}
.main table .fa-2x {
font-size: 1.3rem;
color: var(--yellow);
transition: all 0.3s ease-in-out;
}
.main table .fa-2x:hover {
color: #ffca2c;
transform: scale(1.2);
}
.main table td:nth-child(3) .fa-2x {
color: #dc3545;
}
.main table td:nth-child(3) .fa-2x:hover {
color: #bb2d3b;
}
.main table .fa-circle-check {
color: var(--green);
margin-right: 15px;
transition: all 0.3s ease-in-out;
}
.main table .fa-circle-check:hover {
color: var(--teal);
}
.main table .editable {
background-color: white;
display: inline-block;
padding: 0.3em;
border-radius: 0.3rem;
margin-top: 0.3em;
}
.main .dismissible {
opacity: 0 !important;
height: 0 !important;
padding: 0 !important;
transition: all 0.3s linear;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Task-List</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Fredoka:wght@400;700&family=Lato:wght@100;400;700&family=Smooch+Sans:wght@100;400;700&display=swap" rel="stylesheet" />
<script src="https://kit.fontawesome.com/69ea5f183b.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="css/style.css" />
</head>
<body class="">
<div class="main">
<div class="nav">
<h1>Task List </h1>
<div class="btn-group" role="group" aria-label="Basic example">
<button type="button" class="btn">All</button>
<button type="button" class="btn">To Do</button>
<button type="button" class="btn">Done</button>
</div>
</div>
<div class="alert dismissible">
Please, enter some text.
<i class="fa-solid fa-square-xmark"></i>
</div>
<div class="alert-green dismissible">
Task added successfully.
<i class="fa-solid fa-square-check"></i>
</div>
<form name="new-task">
<label for="task">
<input
type="text"
name="task"
id="task"
placeholder="Add a new task"
/>
<div class="arrow"><i class="fa-solid fa-right-long fa-2xl"></i></div>
</label>
</form>
<div class="container">
<table>
<thead>
<tr>
<th>Don't forget To Do This</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr id="0">
<td>
<i class="fa-solid fa-circle-check fa-2x"></i>
<span class="task" contenteditable="true" data-completed="false"> Learn HTML! </span>
</td>
<td>
<span class="fa-stack fa-2x">
<i class="fa-solid fa-square fa-stack-2x"></i>
<i class="fa-solid fa-pencil fa-stack-1x fa-inverse"></i>
</span>
</td>
<td>
<span class="fa-stack fa-2x">
<i class="fa-solid fa-square fa-stack-2x"></i>
<i class="fa-solid fa-trash fa-stack-1x fa-inverse"></i>
</span>
</td>
</tr>
<tr id="1">
<td>
<i class="fa-solid fa-circle-check fa-2x"></i>
<span class="task" contenteditable="true" data-completed="true"><del>CSS</del></span>
</td>
<td>
<span class="fa-stack fa-2x">
<i class="fa-solid fa-square fa-stack-2x"></i>
<i class="fa-solid fa-pencil fa-stack-1x fa-inverse"></i>
</span>
</td>
<td>
<span class="fa-stack fa-2x">
<i class="fa-solid fa-square fa-stack-2x"></i>
<i class="fa-solid fa-trash fa-stack-1x fa-inverse"></i>
</span>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
<script src="js/app.js"></script>
</html>
Next time please attach a copy of a task object.
You can use filter() function like this :