This repository was archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
85 lines (67 loc) · 2.49 KB
/
script.js
File metadata and controls
85 lines (67 loc) · 2.49 KB
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
document.addEventListener('DOMContentLoaded', () => {
const searchInput = document.getElementById('searchInput');
const tableBody = document.getElementById('tableBody');
async function fetchMaterials() {
try {
const response = await fetch('materials/index.json');
const materials = await response.json();
return materials;
} catch (error) {
console.error('Failed to fetch materials:', error);
}
}
function createTableHeader(keys) {
const thead = document.querySelector('table thead');
const tr = document.createElement('tr');
keys.forEach(key => {
const th = document.createElement('th');
th.textContent = key;
tr.appendChild(th);
});
thead.appendChild(tr);
}
function createTableRow(material) {
const tr = document.createElement('tr');
// Create a table cell for each property
for (const key in material) {
const td = document.createElement('td');
const a = document.createElement('a');
a.href = `product.html?file=${material.material_ID}`;
a.textContent = material[key];
td.appendChild(a);
tr.appendChild(td);
}
return tr;
}
async function populateTable() {
const materials = await fetchMaterials();
if (!materials || materials.length === 0) return;
// Create table header
const keys = Object.keys(materials[0]);
createTableHeader(keys);
// Populate table rows
tableBody.innerHTML = '';
materials.forEach(material => {
const row = createTableRow(material);
tableBody.appendChild(row);
});
// Update row count in header-right
const rowCount = document.getElementById('rowCount');
rowCount.textContent = `${materials.length} materials`;
}
function filterTable(searchText) {
const rows = tableBody.getElementsByTagName('tr');
for (const row of rows) {
const cellText = row.textContent.toLowerCase();
if (cellText.includes(searchText.toLowerCase())) {
row.style.display = '';
} else {
row.style.display = 'none';
}
}
}
searchInput.addEventListener('input', () => {
filterTable(searchInput.value);
});
populateTable();
});