Skip to content

Commit ac0e0f2

Browse files
committed
Add sample UI
1 parent 796de55 commit ac0e0f2

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/main/resources/static/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ <h2>Open</h2>
2121
<table>
2222
<thead>
2323
<tr>
24-
<th>Id</th>
2524
<th>Title</th>
2625
<th>Order</th>
26+
<th>Completed</th>
2727
</tr>
2828
</thead>
2929
<tbody id="tasks"></tbody>
@@ -43,9 +43,9 @@ <h2>New</h2>
4343

4444
<template id="todoRow">
4545
<tr>
46-
<td>a</td>
47-
<td>b</td>
46+
<td>title</td>
4847
<td>0</td>
48+
<td><input type="checkbox" checked></td>
4949
</tr>
5050
</template>
5151

src/main/resources/static/main.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
const http = new XMLHttpRequest();
33

4+
const tbody = document.querySelector("tbody#tasks");
5+
const template = document.querySelector("#todoRow");
6+
const titleInput = document.querySelector("#title");
7+
const orderInput = document.querySelector("#order");
8+
49
function httpSend(method, url, body, callback) {
510
http.open(method, url);
611
http.setRequestHeader('Content-type', 'application/json');
@@ -16,33 +21,41 @@ function httpPost(url, body, callback) {
1621
httpSend('POST', url, body, callback);
1722
}
1823

24+
function addTask(task) {
25+
const clone = template.content.cloneNode(true);
26+
const td = clone.querySelectorAll("td");
27+
const input = clone.querySelectorAll("input");
28+
td[0].textContent = task.title;
29+
td[1].textContent = task.order;
30+
input.checked = task.completed;
31+
tbody.appendChild(clone);
32+
}
33+
1934
function add() {
20-
let body = { title: 'a', order: 0 };
35+
const body = {
36+
title: titleInput.value,
37+
order: orderInput.valueAsNumber
38+
};
39+
2140
httpPost('/tasks', body, () => {
41+
titleInput.value = "";
42+
orderInput.value = 0;
2243

23-
console.log(http.responseText)
44+
addTask(JSON.parse(http.responseText));
2445
});
2546
}
2647

2748
function main() {
28-
const tbody = document.querySelector("tbody#tasks");
29-
const template = document.querySelector("#todoRow");
3049

3150
httpGet('/tasks', null, () => {
32-
let response = JSON.parse(http.responseText);
33-
for (const tr of tbody.children) {
51+
for (const tr of tbody.children)
3452
tr.remove();
35-
}
3653

37-
for (const task of response) {
38-
const clone = template.content.cloneNode(true);
39-
let td = clone.querySelectorAll("td");
40-
td[0].textContent = task.title;
41-
td[1].textContent = task.order;
42-
tbody.appendChild(clone);
43-
}
54+
const response = JSON.parse(http.responseText);
55+
for (const task of response)
56+
addTask(task);
4457

45-
console.log(http.responseText)
58+
console.log(http.responseText);
4659
});
4760
}
4861

0 commit comments

Comments
 (0)