function setupFilterRow(table) {
const table_data = table.getData();
if (!Array.isArray(table_data) || table_data.length === 0) {
console.error("Table data is empty or not an array.");
return;
}
const filterColumns = Object.keys(table_data[0]).map((key) => ({
title: key.charAt(0).toUpperCase() + key.slice(1).replace(/_/g, " "),
field: key
}));
const fieldEl = document.getElementById("filter-field");
const typeEl = document.getElementById("filter-type");
const valueEl = document.getElementById("filter-value");
if (!fieldEl || !typeEl || !valueEl) {
console.error(
"One or more elements not found: filter-field, filter-type, filter-value"
);
return;
}
fieldEl.innerHTML = "";
typeEl.innerHTML = "";
filterColumns.forEach((col) => {
const option = document.createElement("option");
option.value = col.field;
option.textContent = col.title;
fieldEl.appendChild(option);
});
const filterTypes = ["=", "<", "<=", ">", ">=", "!=", "like"];
filterTypes.forEach((type) => {
const option = document.createElement("option");
option.value = type;
option.textContent = type;
typeEl.appendChild(option);
});
function updateFilter() {
const filterVal = fieldEl.value;
const typeVal = typeEl.value;
if (filterVal) {
table.setFilter(filterVal, typeVal, valueEl.value);
}
}
fieldEl.addEventListener("change", updateFilter);
typeEl.addEventListener("change", updateFilter);
valueEl.addEventListener("keyup", updateFilter);
document
.getElementById("filter-clear")
.addEventListener("click", function () {
fieldEl.value = "";
typeEl.value = "=";
valueEl.value = "";
table.clearFilter();
});
}