function displayTable(data, schema) {
const container = htl.html`<div id="example" style="padding-bottom:100px"></div>`;
container.innerHTML = "";
const headers = Object.keys(data[0]);
const columns = headers.map((header) => {
const propertySchema = schema.items.properties[header];
let type = "text";
if (propertySchema) {
if (
propertySchema.type === "integer" ||
propertySchema.type === "number"
) {
type = "numeric";
}
}
return { data: header, type: type };
});
const hot = new Handsontable(container, {
data: data,
columns: columns,
rowHeaders: true,
colHeaders: headers,
height: "auto",
autoWrapRow: true,
autoWrapCol: true,
contextMenu: true,
licenseKey: "non-commercial-and-evaluation"
});
const buttonWrapper = document.createElement("div");
buttonWrapper.style.textAlign = "center";
buttonWrapper.style.marginTop = "20px";
const validateButton = document.createElement("button");
validateButton.innerText = "Validate";
validateButton.addEventListener("click", () => {
validateAgainstSchema(hot, schema, errorMessagesDiv);
});
buttonWrapper.appendChild(validateButton);
const downloadButton = document.createElement("button");
downloadButton.innerText = "Download JSON";
downloadButton.style.marginLeft = "10px";
downloadButton.addEventListener("click", () => {
const editedData = hot.getSourceData();
const jsonData = JSON.stringify(editedData, null, 2);
const blob = new Blob([jsonData], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "data.json";
a.click();
URL.revokeObjectURL(url);
});
buttonWrapper.appendChild(downloadButton);
container.appendChild(buttonWrapper);
const errorContainer = document.createElement("div");
errorContainer.style.border = "1px solid #ccc";
errorContainer.style.padding = "10px";
errorContainer.style.marginTop = "20px";
errorContainer.style.height = "150px";
errorContainer.style.overflowY = "scroll";
errorContainer.style.position = "relative";
const copyButton = document.createElement("button");
copyButton.innerText = "Copy Errors";
copyButton.style.position = "absolute";
copyButton.style.top = "10px";
copyButton.style.right = "10px";
copyButton.addEventListener("click", () => {
navigator.clipboard
.writeText(errorMessagesDiv.innerText)
.then(() => {})
.catch((err) => {
console.error("Failed to copy errors: ", err);
});
});
errorContainer.appendChild(copyButton);
const errorMessagesDiv = document.createElement("div");
errorMessagesDiv.style.marginTop = "40px";
errorMessagesDiv.style.whiteSpace = "pre-wrap";
errorContainer.appendChild(errorMessagesDiv);
container.appendChild(errorContainer);
return container;
}