Vergelijkingstabel
V000-38Maak van een normale beheerbare tabel een vergelijkingstabel dat ook op mobiel gemakkelijk te gebruiken is. Er zullen automatisch knoppen boven de table komen te staan waarmee de bezoeker een kolom kan selecteren om weer te geven. Op mobiel zal standaard de laatste kolom als eerste zichtbaar zijn en deze wordt ook extra met de opmaak uitgelicht.
Het enige wat nodig is om er gebruik van te maken is dat je de opmaak en script over neemt in de website en je de class om een nc-text zet dat veranderd moet worden in de vergelijkingstabel.
HTML<div class="comparison-table"> <nc-text name="table"></nc-text> </div>
CSS.comparison-table table { overflow: hidden; border-radius: calc(var(--panel-radius) / 2); } .comparison-table tbody tr>td { border-top-width: 0; border-left-width: 0; border-right-width: 0; border-color: var(--tertiary-light); padding: calc(var(--panel-padding) / 2); min-width: 8em; } .comparison-table tbody tr:last-child td { border-bottom-width: 0; } .comparison-table tbody tr:first-child>td, .comparison-table tbody tr>td:first-child { font-weight: bold; background-color: var(--tertiary-bg); } .comparison-table tbody tr>td:last-child { background-color: var(--primary-bg); } .comparison-table .column-labels { margin-bottom: 1rem; display: flex; flex-wrap: wrap; justify-content: center; gap: 0.5rem; margin-bottom: 0.5rem; } .comparison-table .column-labels>* { flex-grow: 1; flex-shrink: 0; } .comparison-table [data-hide-cell] { display: none; } @media screen and (min-width: 992px) { .comparison-table tbody tr:last-child>td:first-child { border-bottom-right-radius: calc(var(--panel-radius) / 2); } .comparison-table tbody tr:last-child>td:last-child { border-bottom-left-radius: calc(var(--panel-radius) / 2); } .comparison-table .column-labels { display: none; } .comparison-table [data-hide-cell] { display: table-cell; } }
JSonInteractive(() => { const HIDDEN_ATTRIBUTE_NAME = 'data-hide-cell' const DEFAULT_STYLE = 'tertiary' const IMPORTANT_STYLE = 'primary' const setupTable = ( tableWrapperElement, ) => { if (!tableWrapperElement.classList.contains('.table-responsive')) { let tempElement = tableWrapperElement.closest('.table-responsive') if (tempElement) { tableWrapperElement = tempElement } else { tempElement = tableWrapperElement.querySelector('.table-responsive') if (tempElement) { tableWrapperElement = tempElement } } } const table = tableWrapperElement.querySelector('table') const columnLabelsElement = document.createElement('div') columnLabelsElement.setAttribute('class', 'column-labels') tableWrapperElement.insertAdjacentElement('afterbegin', columnLabelsElement) let headings = Array.from( table.querySelectorAll('thead>tr>th'), ) if (headings.length === 0) { headings = Array.from( table.querySelectorAll( 'tbody>tr:first-child>td', ) ) } const setupHeading = ( heading, headingIndex, ) => { const headingText = heading.textContent.trim() if (headingText.length === 0) { return } const headingButton = document.createElement('button') headingButton.setAttribute('type', 'button') headingButton.setAttribute('class', 'column-label btn btn-' + DEFAULT_STYLE) headingButton.textContent = headingText columnLabelsElement.appendChild(headingButton) headingButton.addEventListener('click', () => { let allCells = Array.from( table.querySelectorAll('tbody>tr>td:not(:first-child)') ) for (let i = 0; i < allCells.length; i++) { allCells[i].setAttribute(HIDDEN_ATTRIBUTE_NAME, true) } let visibleCells = Array.from( table.querySelectorAll('thead>tr>*:nth-child(' + (headingIndex + 1) + '),tbody>tr>td:nth-child(' + (headingIndex + 1) + ')') ) for (let i = 0; i < visibleCells.length; i++) { visibleCells[i].removeAttribute(HIDDEN_ATTRIBUTE_NAME) } }) } for (let i = 0; i < headings.length; i++) { setupHeading(headings[i], i) } const lastButton = columnLabelsElement.querySelector('.column-label:last-child') if (lastButton) { lastButton.classList.remove('btn-' + DEFAULT_STYLE) lastButton.classList.add('btn-' + IMPORTANT_STYLE) lastButton.click() } } const tableElements = Array.from( document.querySelectorAll('.comparison-table'), ) for (let i = 0; i < tableElements.length; i++) { setupTable(tableElements[i]) } })