Skip to content

Commit c25ba2e

Browse files
committed
add todo list spa
1 parent f63d033 commit c25ba2e

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

spa/index.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<style>
10+
.striked {
11+
text-decoration: line-through;
12+
}
13+
</style>
14+
<body>
15+
<input id="newTodo" type="text" placeholder="Add new todo">
16+
<button onclick="addTodo()"> ADD +</button>
17+
18+
<ul>
19+
20+
</ul>
21+
22+
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
23+
<script src="index.js" defer></script>
24+
</body>
25+
</html>

spa/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const todos = [
2+
{
3+
id: 1,
4+
name: "Teach Class at Nagarro",
5+
done: true
6+
},
7+
{
8+
id: 2,
9+
name: "Get Coffee",
10+
done: false
11+
}
12+
];
13+
14+
function render(state) {
15+
return state
16+
.map(todo => {
17+
const classString = todo.done ? `class = "striked"` : ''
18+
return `<li data-todo="${todo.id}" ${classString}> ${todo.name} </li>`;
19+
})
20+
.join("");
21+
}
22+
23+
function paint() {
24+
$("ul").html(render(todos));
25+
}
26+
27+
function addTodo() {
28+
// document.getElementById('newTodo') != $('#newTodo')
29+
const inputBox = $('#newTodo')
30+
todos.push({
31+
id: todos.length + 1,
32+
name: inputBox.val(),
33+
done: false
34+
})
35+
36+
inputBox.val('')
37+
38+
paint()
39+
}
40+
41+
42+
$('ul').on("click", function (e) {
43+
const idToFind = e.target.dataset.todo
44+
const todo = todos.find(todo => todo.id == idToFind)
45+
todo.done = !todo.done
46+
47+
paint()
48+
})
49+
50+
paint();

0 commit comments

Comments
 (0)