-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (38 loc) · 1.62 KB
/
index.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
var items;
var input = document.getElementById('new-item');
var url = "http://localhost:3000/todo/";
async function getItem() {
await axios.get(url).then(res => {return items = res.data}).then(() => {
render(items)});
input.value = '';
}
getItem();
async function addItem() {
//var input = document.getElementById('new-item');
await axios.post(url, {content: input.value});
getItem();
}
var addBtn = document.getElementById('add-btn');
addBtn.addEventListener('click', addItem);
async function delItem() {
id = parseInt(event.target.dataset.value);
console.log(id);
await axios.delete(url + id);
getItem();
render(items);
}
async function editItem() {
id = parseInt(event.target.dataset.value);
console.log(id);
await axios.put(url + id, {content: input.value});
getItem();
render(items);
input.value = '';
}
function render(items) {
var htmlList = document.getElementById("todo-list");
var content = items.map(function(item, index) {
return '<li>' + item.content + ' ' + '<button onclick = "editItem()", data-value ="' + item.id + '">Edit</button><button onClick ="delItem()", data-value ="' + item.id + '">Delete</button>' + '</li>' ;
});
htmlList.innerHTML = content.join('');
}