← Code-Übersicht

tabelle.js

Pfad: public/assets/tabelle.js

Ext: js

Größe: 1834 Bytes

Geändert: 2026-07-15 22:42:55+02

// Generische Tabellen: clientseitiges Filtern und Sortieren fuer jede .tabelle.
// Filter: <input data-filter="#tabellen-id">. Sortieren: <th data-sort> im thead.
(function () {
    'use strict';

    Array.prototype.forEach.call(document.querySelectorAll('input[data-filter]'), function (input) {
        var ziel = document.querySelector(input.getAttribute('data-filter'));
        if (!ziel || !ziel.tBodies.length) {
            return;
        }
        var tbody = ziel.tBodies[0];
        input.addEventListener('input', function () {
            var q = input.value.toLowerCase();
            Array.prototype.forEach.call(tbody.rows, function (row) {
                row.style.display = row.textContent.toLowerCase().indexOf(q) !== -1 ? '' : 'none';
            });
        });
    });

    Array.prototype.forEach.call(document.querySelectorAll('table.tabelle'), function (table) {
        if (!table.tHead || !table.tBodies.length) {
            return;
        }
        var tbody = table.tBodies[0];
        Array.prototype.forEach.call(table.tHead.rows[0].cells, function (th, index) {
            if (!th.dataset.sort) {
                return;
            }
            var aufsteigend = true;
            th.addEventListener('click', function () {
                var zeilen = Array.prototype.slice.call(tbody.rows);
                zeilen.sort(function (a, b) {
                    var x = a.cells[index].textContent.trim().toLowerCase();
                    var y = b.cells[index].textContent.trim().toLowerCase();
                    return aufsteigend ? x.localeCompare(y) : y.localeCompare(x);
                });
                aufsteigend = !aufsteigend;
                zeilen.forEach(function (row) {
                    tbody.appendChild(row);
                });
            });
        });
    });
}());