-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (61 loc) · 2.26 KB
/
script.js
File metadata and controls
71 lines (61 loc) · 2.26 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
let allTodos = [];
async function fetchTodos() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/todos');//
const data = await response.json()
// agar sabhi data ko dekhna ho to data.slice ko (0,200) set kar dena hoga
allTodos = data.slice(0, 50);
renderTable(allTodos);
} catch (error) {
console.error('Error fetching data:', error);
}
}
function renderTable(todos) {
const tbody = document.getElementById('body');
tbody.innerHTML = '';
todos.forEach(todo => {
const row = document.createElement('tr');
if (todo.completed) {
row.classList.add('row-completed');
} else {
row.classList.add('row-incomplete');
}
row.innerHTML = `
<td>${todo.id}</td>
<td>${todo.title}</td>
<td>${todo.completed ? 'Completed' : 'Pending'}</td>
<td>
<button class="toggle-btn" onclick="toggleTaskStatus(${todo.id})">
Mark as ${todo.completed ? 'Incomplete' : 'Completed'}
</button>
</td>
`;
tbody.appendChild(row);
});
}
function toggleTaskStatus(id) {
const todo = allTodos.find(t => t.id === id);
if (todo) {
todo.completed = !todo.completed;
const activeFilter = document.querySelector('.filter-btn.active').id;
if(activeFilter === 'btn-completed') filterTodos('completed');
else if(activeFilter === 'btn-uncompleted') filterTodos('uncompleted');
else filterTodos('all');
}
}
function filterTodos(status) {
document.querySelectorAll('.filter-btn').forEach(btn => btn.classList.remove('active'));
let filteredData = [];
if (status === 'completed') {
document.getElementById('btn-completed').classList.add('active');
filteredData = allTodos.filter(todo => todo.completed === true);
} else if (status === 'uncompleted') {
document.getElementById('btn-uncompleted').classList.add('active');
filteredData = allTodos.filter(todo => todo.completed === false);
} else {
document.getElementById('btn-all').classList.add('active');
filteredData = allTodos;
}
renderTable(filteredData);
}
fetchTodos();