baseddata.io/layouts/partials/table.html

132 lines
4.0 KiB
HTML
Raw Normal View History

2024-09-22 14:27:13 +01:00
<script>
function createTable(
endpoint,
id,
headers,
maxHeight,
sortable,
valueId,
selectableRows,
) {
2024-09-23 17:46:59 +01:00
async function fetchDataForTable(query) {
2024-09-22 14:27:13 +01:00
try {
2024-09-23 17:46:59 +01:00
const apiEndpoint = `${apiURL}/${endpoint}?${query}`;
2024-09-22 14:27:13 +01:00
const response = await fetch(apiEndpoint);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const fetchedData = await response.json();
data = fetchedData;
generateTable(data);
} catch (error) {
console.error("Fetching data failed:", error);
}
}
function generateTable(data) {
const jsonTableContainer = document.getElementById(
2024-09-23 17:46:59 +01:00
`${id}--table-container`,
2024-09-22 14:27:13 +01:00
);
jsonTableContainer.className = "jsonTableContainer";
jsonTableContainer.innerHTML = "";
jsonTableContainer.style.maxHeight = maxHeight;
tableHeaderNames = Object.values(headers);
tableHeaderKeys = Object.keys(headers);
const table = document.createElement("table");
2024-09-23 17:46:59 +01:00
table.id = `${id}--table`;
2024-09-22 14:27:13 +01:00
const thead = document.createElement("thead");
const tbody = document.createElement("tbody");
const headerRow = document.createElement("tr");
tableHeaderNames.forEach((header) => {
const th = document.createElement("th");
th.textContent = header;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
for (const key in data) {
const row = document.createElement("tr");
row.value = data[key][valueId];
tableHeaderKeys.forEach((columnName) => {
const td = document.createElement("td");
const div = document.createElement("div");
div.id = "scrollable";
div.textContent = data[key][columnName];
td.appendChild(div);
row.appendChild(td);
tbody.appendChild(row);
});
}
table.appendChild(thead);
table.appendChild(tbody);
// selectable rows
let currentlySelectedRow = null;
if (selectableRows === "multi" || selectableRows === "single") {
const rows = table.getElementsByTagName("tr");
for (let i = 1; i < rows.length; i++) {
// Skip the header row
rows[i].addEventListener("click", function () {
if (selectableRows === "single") {
// Deselect the currently selected row, if any
if (currentlySelectedRow && currentlySelectedRow !== this) {
currentlySelectedRow.classList.remove("selected");
const deselectEvent = new CustomEvent("rowDeselected", {
detail: { row: currentlySelectedRow },
});
document.dispatchEvent(deselectEvent);
}
// Toggle the selected class on the clicked row
this.classList.toggle("selected");
// Update the currently selected row
currentlySelectedRow = this.classList.contains("selected")
? this
: null;
} else {
// Multi-select behavior
this.classList.toggle("selected");
}
const event = new CustomEvent(
this.classList.contains("selected")
? "rowSelected"
: "rowDeselected",
{
2024-09-23 17:46:59 +01:00
detail: {
row: this,
selectableRows: selectableRows,
valueId: valueId,
},
2024-09-22 14:27:13 +01:00
},
);
document.dispatchEvent(event);
});
}
}
jsonTableContainer.appendChild(table);
2024-09-23 17:46:59 +01:00
// sortable
2024-09-22 14:27:13 +01:00
if (sortable == "true") {
table.className = "sortable";
2024-09-23 17:46:59 +01:00
sorttable.makeSortable(document.getElementById(`${id}--table`));
2024-09-22 14:27:13 +01:00
}
}
2024-09-23 17:46:59 +01:00
document.addEventListener("filterChange", function (event) {
query = queryConstructor()
fetchDataForTable(query);
});
query = queryConstructor()
fetchDataForTable(query);
2024-09-22 14:27:13 +01:00
}
</script>