-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_csv4.html
96 lines (86 loc) · 3.38 KB
/
demo_csv4.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
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSV Textarea</title>
<script type="module" src="csvtextarea.js"></script>
</head>
<body>
<p>Testing how the csv-textarea works when both the inner textarea has an id and name when the outer csv-textarea differnent id and name.</p>
<form id="csv-form">
<csv-textarea
id="csvDataToo"
name="csvDataToo"
rows="3"
column-headings="Name,Age,City"
title="Custom CSV Textarea"
debug="true"
>
<textarea id="csvData" name="csvData">
John,30,New York
Jane,25,Los Angeles
</textarea>
</csv-textarea>
<button type="submit">Submit</button>
</form>
<script>
document.addEventListener('DOMContentLoaded', () => {
const csvTextarea = document.querySelector('csv-textarea');
// Listen for change events on the csv-textarea
csvTextarea.addEventListener('cell-change', (event) => {
console.log('Cell changed:', event.detail);
});
// Convert CSV content to JSON
const jsonButton = document.createElement('button');
jsonButton.textContent = 'Convert to JSON';
jsonButton.addEventListener('click', () => {
console.log('JSON content:', csvTextarea.toJSON());
});
document.body.appendChild(jsonButton);
// Update a specific cell by column name
const updateByNameButton = document.createElement('button');
updateByNameButton.textContent = 'Update Cell (1, "Age") to "New Value"';
updateByNameButton.addEventListener('click', () => {
csvTextarea.setCellValue(1, 'Age', 'New Value');
});
document.body.appendChild(updateByNameButton);
// Update a specific cell by column number
const updateByNumberButton = document.createElement('button');
updateByNumberButton.textContent = 'Update Cell (1, 2) to "Another Value"';
updateByNumberButton.addEventListener('click', () => {
csvTextarea.setCellValue(1, 2, 'Another Value');
});
document.body.appendChild(updateByNumberButton);
// Get a specific cell value by column name
const getByNameButton = document.createElement('button');
getByNameButton.textContent = 'Get Cell Value (1, "Age")';
getByNameButton.addEventListener('click', () => {
const value = csvTextarea.getCellValue(1, 'Age');
console.log('Cell value:', value);
});
document.body.appendChild(getByNameButton);
// Get a specific cell value by column number
const getByNumberButton = document.createElement('button');
getByNumberButton.textContent = 'Get Cell Value (1, 2)';
getByNumberButton.addEventListener('click', () => {
const value = csvTextarea.getCellValue(1, 2);
console.log('Cell value:', value);
});
document.body.appendChild(getByNumberButton);
});
let csvForm = document.getElementById('csv-form');
csvForm.addEventListener('submit', function (event) {
event.preventDefault();
const csvComponent = document.getElementById('csvDataToo');
const csvData = csvComponent.toCSV();
const formData = new FormData();
formData.set('groups', csvData);
// Display the submitted form data.
for (const pair of formData.entries()) {
console.log(pair[0], pair[1]);
}
});
</script>
</body>
</html>