-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.html
54 lines (47 loc) · 1.22 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<title>Live table</title>
<style>
input { border: 1px solid green; width: 30px; }
</style>
<body>
<h1>Live table</h1>
<table id="table"></table>
<script>
let table = document.getElementById('table');
let cells = {};
let letters = ['A', 'B', 'C', 'D', 'E', 'F'];
let tr;
tr = document.createElement('tr');
tr.innerHTML = '<td></td>' + letters
.map(col => '<td>' + col + '</td>')
.join('');
table.appendChild(tr);
for (let i = 1; i <= 5; i++) {
tr = document.createElement('tr');
tr.innerHTML = '<td>' + i + '</td>' + letters
.map(col => '<td><input id="' + col + i + '" type="text"></td>')
.join('');
table.appendChild(tr);
letters.forEach(col => {
let cell = col + i;
let input = document.getElementById(cell);
input.addEventListener('keyup', keyup);
cells[cell] = input;
});
}
function keyup(event) {
socket.send(JSON.stringify({
cell: event.target.id,
value: event.target.value
}));
}
let socket = new WebSocket('ws://127.0.0.1/');
socket.onmessage = event => {
let change = JSON.parse(event.data);
let cell = cells[change.cell];
cell.value = change.value;
};
</script>
</body>
</html>