Jump to content

MediaWiki:Common.js: Difference between revisions

From Carbon Accounting Alliance Wiki
WikiSysop (talk | contribs)
No edit summary
Tag: Reverted
WikiSysop (talk | contribs)
No edit summary
Tag: Reverted
Line 2: Line 2:


#Filterable tables
#Filterable tables
document.addEventListener("DOMContentLoaded", function () {
mw.loader.using(['jquery'], function () {
     function createFilterInputs(table) {
     $(document).ready(function () {
        let thead = table.querySelector("thead");
        function addFiltersToTable(table) {
        if (!thead) {
            var thead = $(table).find("thead");
            console.warn("No <thead> found, creating one...");
            if (thead.length === 0) {
             let headerRow = table.rows[0];
                // If no thead exists, create one from the first row
            if (!headerRow) return;
                var firstRow = $(table).find("tr:first");
             thead = document.createElement("thead");
                thead = $("<thead></thead>").append(firstRow.clone());
             thead.appendChild(headerRow.cloneNode(true));
                $(table).prepend(thead);
            table.insertBefore(thead, table.firstChild);
             }
        }
 
            var headerRow = thead.find("tr:first");
             var filterRow = $("<tr></tr>");
 
             headerRow.find("th").each(function (colIndex) {
                var filterCell = $("<th></th>");
                var input = $("<input>").attr({
                    "type": "text",
                    "placeholder": "Filter...",
                    "class": "table-filter"
                });


        let filterRow = document.createElement("tr");
                input.on("keyup", function () {
                    var filterText = $(this).val().toLowerCase();
                    $(table).find("tbody tr").each(function () {
                        var cellText = $(this).find("td").eq(colIndex).text().toLowerCase();
                        $(this).toggle(cellText.indexOf(filterText) > -1);
                    });
                });


        for (let i = 0; i < thead.rows[0].cells.length; i++) {
                filterCell.append(input);
            let th = document.createElement("th");
                filterRow.append(filterCell);
             let input = document.createElement("input");
             });


             input.setAttribute("type", "text");
             thead.append(filterRow);
            input.setAttribute("placeholder", "Filter...");
        }
            input.style.width = "90%";
            input.dataset.column = i;


            input.addEventListener("keyup", function () {
        // Apply to all tables with class 'filterable-table'
                let colIndex =
        $("table.filterable-table").each(function () {
            addFiltersToTable(this);
        });
    });
});

Revision as of 21:03, 11 March 2025

/* Any JavaScript here will be loaded for all users on every page load. */

#Filterable tables
mw.loader.using(['jquery'], function () {
    $(document).ready(function () {
        function addFiltersToTable(table) {
            var thead = $(table).find("thead");
            if (thead.length === 0) {
                // If no thead exists, create one from the first row
                var firstRow = $(table).find("tr:first");
                thead = $("<thead></thead>").append(firstRow.clone());
                $(table).prepend(thead);
            }

            var headerRow = thead.find("tr:first");
            var filterRow = $("<tr></tr>");

            headerRow.find("th").each(function (colIndex) {
                var filterCell = $("<th></th>");
                var input = $("<input>").attr({
                    "type": "text",
                    "placeholder": "Filter...",
                    "class": "table-filter"
                });

                input.on("keyup", function () {
                    var filterText = $(this).val().toLowerCase();
                    $(table).find("tbody tr").each(function () {
                        var cellText = $(this).find("td").eq(colIndex).text().toLowerCase();
                        $(this).toggle(cellText.indexOf(filterText) > -1);
                    });
                });

                filterCell.append(input);
                filterRow.append(filterCell);
            });

            thead.append(filterRow);
        }

        // Apply to all tables with class 'filterable-table'
        $("table.filterable-table").each(function () {
            addFiltersToTable(this);
        });
    });
});