Files
openfoodnetwork/app/webpacker/controllers/column_preferences_controller.js
David Cook 70fab2bcc1 Show/hide columns using display instead of visibility
Visibility was way simpler, but the table doesn't recalculate column widths until you use display:none;

This is now using the same method as the old products screen.
But we still need to update colspans..
2024-06-13 10:08:56 +10:00

53 lines
1.5 KiB
JavaScript

import { Controller } from "stimulus";
// Manage column visibility according to checkbox selection
//
export default class ColumnPreferencesController extends Controller {
connect() {
this.checkboxes = this.element.querySelectorAll("input[type=checkbox]");
for (const element of this.checkboxes) {
// On initial load
this.#showHideColumn(element);
// On checkbox changed
element.addEventListener("change", this.#showHideColumn.bind(this));
}
}
// private
#showHideColumn(e) {
const element = e.target || e;
const name = element.dataset.columnName;
const selector = `col[data-column-preferences-name="${name}"]`;
const column = document.querySelector(selector);
const index = this.#getIndex(column);
if (column == null) {
console.error(`ColumnPreferencesController: could not find ${selector}`);
return;
}
// Hide column definition
this.#showHideElement(column, element.checked);
// Hide each cell in column (ignore rows with colspan)
const rows = column.closest("table").querySelectorAll("tr:not(:has(td[colspan]))");
rows.forEach((row) => {
// Ignore cell if spanning multiple columns
const cell = row.children[index];
if (cell == undefined) return;
this.#showHideElement(cell, element.checked);
});
}
#getIndex(column) {
return Array.from(column.parentNode.children).indexOf(column);
}
#showHideElement(element, show) {
element.style.display = show ? "" : "none";
}
}