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..
This commit is contained in:
David Cook
2024-06-12 15:55:13 +10:00
parent 0190d6f31d
commit 70fab2bcc1
2 changed files with 24 additions and 5 deletions

View File

@@ -1,11 +1,10 @@
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]');
this.checkboxes = this.element.querySelectorAll("input[type=checkbox]");
for (const element of this.checkboxes) {
// On initial load
@@ -22,12 +21,32 @@ export default class ColumnPreferencesController extends Controller {
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;
}
column.style.visibility = (element.checked ? '' : 'collapse');
// 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";
}
}

View File

@@ -49,7 +49,7 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi
within ofn_drop_down("Columns") do
uncheck "Name"
end
# expect(page).not_to have_selector "th", text: "Name" # column is not visible, but capybara doesn't understand yet.
expect(page).not_to have_selector "th", text: "Name"
expect_other_columns_visible
# Preference saved
@@ -62,7 +62,7 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi
within ofn_drop_down("Columns") do
expect(page).to have_unchecked_field "Name"
end
# expect(page).not_to have_selector "th", text: "Name"
expect(page).not_to have_selector "th", text: "Name"
expect_other_columns_visible
end