forked from contentful-labs/ui-editable-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
87 lines (75 loc) · 2.17 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Contentful UI extension country select</title>
<link rel="stylesheet" href="https://contentful.github.io/ui-extensions-sdk/cf-extension.css">
<script src="https://contentful.github.io/ui-extensions-sdk/cf-extension-api.js"></script>
<style>
input {
border: none;
width: 100%;
font-size: 14px;
padding: 2px;
text-align: center;
}
input:hover { background-color: #eee; }
input:focus { background-color: #ccf; }
table {
border-collapse: collapse;
width: 100%;
}
td {
border: 1px solid #999;
padding: 0;
}
tr:first-child td input {
background-color: #ccc;
font-weight: bold;
text-align: center;
user-select: none;
}
</style>
</head>
<body>
<div id="content">
<table></table>
</div>
<script>
function getInitialData({rows, header}) {
const data = [
header
];
const columns = data[0].length;
for (let i = 0; i <= rows; i++) {
data.push(new Array(data[0].length));
}
return data;
}
function createDOMTable(elem, tableData) {
for (let i = 0; i < tableData.length; i++) {
let row = elem.insertRow();
for (let j = 0; j < tableData[0].length; j++) {
row.insertCell().innerHTML = `<input data-row="${i}" data-column="${j}" value="${tableData[i][j] || ''}"/>`;
}
}
}
window.contentfulExtension.init(extension => {
let value = extension.field.getValue()
if (!value) {
value = {
tableData : getInitialData({
rows: 3,
header: ['FOO', 'BAR', 'BAZ']
})
};
}
createDOMTable(document.querySelector('table'), value.tableData);
window.addEventListener('blur', e => {
value.tableData[e.target.dataset.row][e.target.dataset.column] = e.target.value;
extension.field.setValue(value);
}, true);
});
</script>
</body>
</html>