-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
130 lines (121 loc) · 4.62 KB
/
main.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const fileInput = document.getElementById("file-input");
fileInput.addEventListener('change', () => {
const files = fileInput.files;
handleFiles(files);
});
async function handleFiles(files) {
const results = [];
const headers = [];
for (const file of files) {
const reader = new FileReader();
const promise = new Promise((resolve, reject) => {
reader.onload = () => {
const data = new Uint8Array(reader.result);
const workbook = XLSX.read(data, { type: 'array' });
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
const rows = XLSX.utils.sheet_to_json(worksheet, { defval: "" });
const dateColumns = Object.keys(rows[0]);
headers.push(new Set(dateColumns))
results.push({ name: file.name, data: rows });
resolve();
};
reader.onerror = () => {
reject(reader.error);
};
});
reader.readAsArrayBuffer(file);
await promise;
}
prepareTable(results);
const commonColumns = headers.reduce((prev, current) => {
if (prev.size === 0) {
return current;
} else {
const intersection = new Set([...prev].filter(x => current.has(x)));
return intersection;
}
});
const container = document.getElementById('filters');
commonColumns.forEach(value => {
const input = document.createElement('input');
input.type = "text"
input.classList.add("filter")
input.placeholder = value
container.append(input)
});
const br = document.createElement('br');
container.appendChild(br);
const button = document.createElement('button');
button.type = "button";
button.onclick = filter;
button.innerText = "Submit";
container.appendChild(button);
}
function prepareTable(results) {
const container = document.getElementById('result');
container.innerHTML = '';
results.forEach(result => {
const table = document.createElement('table');
const caption = document.createElement('caption');
caption.innerText = result.name;
table.appendChild(caption);
const headerRow = document.createElement('tr');
for (const key in result.data[0]) {
const headerCell = document.createElement('th');
headerCell.innerText = key;
headerRow.appendChild(headerCell);
}
table.appendChild(headerRow);
result.data.forEach(row => {
const tableRow = document.createElement('tr');
for (const key in row) {
const tableCell = document.createElement('td');
tableCell.innerText = row[key];
tableRow.appendChild(tableCell);
}
tableRow.style.display = "none";
table.appendChild(tableRow);
});
container.appendChild(table);
});
}
function filter() {
var filters = Array.from(document.querySelectorAll(".filter")).map(input => input.value.toUpperCase());
var columnName = Array.from(document.querySelectorAll(".filter")).map(input => input.placeholder);
// Declare variables
var tables, tr, td, i, txtValue;
tables = document.getElementById("result").querySelectorAll("table");
tables.forEach(table => {
var headerRow = table.rows[0];
const indices = columnName.map(name => {
// Loop through each cell in the header row
for (let i = 0; i < headerRow.cells.length; i++) {
if (headerRow.cells[i].innerText === name) {
return i;
}
}
// Return -1 if no match was found
return -1;
});
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and show all the rows
for (i = 0; i < tr.length; i++) {
tr[i].style.display = "";
}
for (let j = 0; j < indices.length; j++) {
if (filters[j] !== "") {
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[indices[j]];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filters[j]) === -1) {
tr[i].style.display = "none";
}
}
}
}
}
});
}