Files
openfoodnetwork/app/webpacker/controllers/column_preferences_controller.js
David Cook d81c3cb489 Show/hide columns based on checkboxes
The cols could have been a lot cleaner with simple classnames, but I preferred to mark up in a way that reveals the purpose (otherwise they could be used for styling).

It doesn't seem to be any faster comparing querySelector('[data]') vs class, or iterating through the dom nodes.
2024-06-12 14:58:00 +10:00

34 lines
927 B
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);
if (column == null) {
console.error(`ColumnPreferencesController: could not find ${selector}`);
return;
}
column.style.visibility = (element.checked ? '' : 'collapse');
}
}