-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
111 lines (109 loc) · 3.3 KB
/
script.js
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
100
101
102
103
104
105
106
107
108
109
110
111
let todoItems = [];
let updateId;
function fetchTodos() {
$.ajax({
type: 'GET',
data: { data: 1 },
url: "read.php",
success: function (data) {
todoItems = JSON.parse(data);
displayTodos();
}
});
}
$(document).ready(function () {
fetchTodos();
$('#todoAdd').click(function (e) {
if ($('#todo').val() != "") {
e.preventDefault();
$.ajax({
type: 'POST',
data: { data: $('#todo').val() },
url: "create.php",
success: function (data) {
fetchTodos();
$('#todo').val("");
}
});
}
else
alert("Please fill the Todo");
});
$('#updateBtn').click(function (e) {
if ($('#todo').val() != "") {
e.preventDefault();
$.ajax({
type: 'POST',
data: { todoId: updateId, data: $('#todo').val() },
url: "update.php",
success: function (data) {
console.log("updated");
$('#todoAdd').removeClass('d-none');
$('#updateBtn').addClass('d-none');
fetchTodos();
$('#todo').val("");
}
});
}
else
alert("Please fill the todo");
});
})
function displayTodos() {
$('#myTodos').empty();
todoItems.map((item, key) => {
document.getElementById('myTodos').innerHTML += `
<div class="row todoItem rounded p-2" >
<div class="col-lg-8 col-sm-6">
<p class="text-center text-truncate" onclick="changeAction(!${item.todoAction},${item.todoId})" id="todoText${key}">${item.todoItem}</p>
</div>
<div class="col-lg-4 col-sm-6 d-flex justify-content-center">
<button class="btn btn-primary m-1 editBtn${key}" onclick="editTodo(${parseInt(item.todoId)})"><i class="bi bi-pencil-square"></i></button>
<button class="btn btn-danger m-1" onclick="deleteTodo(${item.todoId})"><i class="bi bi-trash-fill"></i></button>
</div>
</div>
`;
if (item.todoAction == 1) {
$("#todoText" + key).css("text-decoration", "line-through");
$(".editBtn" + key).attr('disabled', true);
}
else {
$("#todoText" + key).css("text-decoration", "none");
$(".editBtn" + key).removeAttr('disabled');
}
});
}
function changeAction(actionKey, todoId) {
$.ajax({
type: 'POST',
data: { todoId: todoId, actionKey: actionKey },
url: "action.php",
success: function (data) {
fetchTodos();
}
});
}
function editTodo(todoId) {
$.ajax({
type: 'GET',
data: { todoId: todoId },
url: "update.php",
success: function (data) {
$('#todo').val(data);
$('#todoAdd').addClass('d-none');
$('#updateBtn').removeClass('d-none');
$(window).scrollTop(0);
updateId = todoId;
}
});
}
function deleteTodo(todoId) {
$.ajax({
type: 'POST',
data: { todoId: todoId },
url: "delete.php",
success: function (data) {
fetchTodos();
}
});
}