-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (92 loc) · 3.99 KB
/
Copy pathscript.js
File metadata and controls
99 lines (92 loc) · 3.99 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
93
94
95
96
97
98
99
document.addEventListener("DOMContentLoaded", function() {
let title = document.getElementById("title");
let desc = document.getElementById("desc");
let submit = document.getElementById("submit");
function toAddInLocalStorage() {
let tValue = title.value;
let dValue = desc.value;
if (tValue && dValue) {
localStorage.setItem(tValue, dValue);
title.value = "";
desc.value = "";
toShowInTable();
} else {
alert("Please fill in both Title and Description.");
}
}
submit.addEventListener('click', toAddInLocalStorage);
function toShowInTable() {
let tableBody = document.querySelector("tbody");
tableBody.innerHTML = ""; // Clear existing rows
let tnum = 0;
for (let i = 0; i < localStorage.length; i++) {
tnum++;
let key = localStorage.key(i);
let value = localStorage.getItem(key);
let rowHTML = `
<tr class="main-row">
<th scope="row">${tnum}</th>
<td>${key}</td>
<td>${value}</td>
<td>
<input class="form-check-input" type="checkbox" value="">
</td>
</tr>
<tr class="detail-row">
<td colspan="4">
<div class="hidden-buttons">
<span class="edit-buttons">
<button class="edit-btn btn btn-secondary btn-sm">Edit</button>
<button class="delete-btn btn btn-danger btn-sm">Delete</button>
</span>
</div>
</td>
</tr>
`;
tableBody.innerHTML += rowHTML; // Add rows to the table
}
addRowClickEvents(); // Add click events to the rows
}
function addRowClickEvents() {
let rows = document.querySelectorAll("tbody tr.main-row"); // Get all main rows in the table body
rows.forEach(row => { // Loop through each main row
row.addEventListener('click', function (e) { // Add click event listener to the main row
if (e.target.className !== 'form-check-input' && !e.target.classList.contains('edit-btn') && !e.target.classList.contains('delete-btn')) {
row.classList.toggle('row-selected'); // Toggle 'row-selected' class
let detailRow = row.nextElementSibling;
if (row.classList.contains('row-selected')) {
detailRow.style.display = 'table-row';
setTimeout(() => {
detailRow.style.height = '70px'; // Adjusted height for larger sliding effect
detailRow.querySelector('.hidden-buttons').style.display = 'flex';
detailRow.querySelector('.hidden-buttons').style.opacity = '1';
}, 0);
} else {
detailRow.querySelector('.hidden-buttons').style.opacity = '0';
setTimeout(() => {
detailRow.style.height = '0';
detailRow.style.display = 'none';
detailRow.querySelector('.hidden-buttons').style.display = 'none';
}, 500); // Match transition duration with CSS
}
}
});
// Edit button event listener
row.nextElementSibling.querySelector('.edit-btn').addEventListener('click', function () {
let key = row.cells[1].innerText;
let value = row.cells[2].innerText;
title.value = key;
desc.value = value;
localStorage.removeItem(key); // Remove item from local storage
toShowInTable(); // Update the table
});
// Delete button event listener
row.nextElementSibling.querySelector('.delete-btn').addEventListener('click', function () {
let key = row.cells[1].innerText;
localStorage.removeItem(key); // Remove item from local storage
toShowInTable(); // Update the table
});
});
}
toShowInTable(); // Show table rows on page load
})