Skip to main content
Participating Frequently
March 4, 2023
Question

Hide HTML table until a dropdown selection has been made

  • March 4, 2023
  • 1 reply
  • 234 views

Hi! I have an HTML table that loads onto the page from a local CSV file. I also have 2 dropdown selectors that filter the loaded table by "Region" and "Store Owner". I have a script that "hides" the table results if the dropdowns are on their default states of "Select a Region" and "Select an Owner/Manager". 

My issue is that when the page loads, it still displays all the table results. Any idea how to hide the tableon page load, and only display the results once the user changes the drop-down options? Here is my rurrent script. 

<!DOCTYPE html>
<html>
<head>
    <title>CSV Table Filter</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- <style>
        #badcock_table {
           display: none;
        }
     </style> -->
</head>

<body>
    <label for="filter-region">Region:</label>
    <select id="filter-region">
        <option value="">Select a Region</option>
    </select>
    <label for="filter-owner">Owner/Manager:</label>
    <select id="filter-owner">
        <option value="">Select an Owner/Manager</option>
    </select>
    <div id="badcock_table">
        <table id="csv-table">
        </table>
    </div>
    <script>
        
        window.onload = function () {
            const table = document.getElementById("csv-table");
            const filterRegion = document.getElementById("filter-region");
            const filterOwner = document.getElementById("filter-owner");
            const valuesRegion = {};
            const valuesStatus = new Set();
            fetch("data/data.csv")
                .then(response => response.text())
                .then(data => {
                    const rows = data.split("\n");
                    for (let i = 0; i < rows.length; i++) {
                        const cells = rows[i].split(",");
                        if (cells.length > 1) {
                            const row = table.insertRow(-1);
                            if (i === 0) {
                                for (let j = 0; j < cells.length; j++) {
                                    const headerCell = document.createElement("th");
                                    headerCell.appendChild(document.createTextNode(cells[j]));
                                    row.appendChild(headerCell);
                                }
                            } else {
                                for (let j = 0; j < cells.length; j++) {
                                    const cell = row.insertCell(j);
                                    cell.appendChild(document.createTextNode(cells[j]));
                                }
                                valuesRegion[cells[0]] = true;
                                valuesStatus.add(cells[2]);
                            }
                        }
                    }
                    for (const region in valuesRegion) {
                        const option = document.createElement("option");
                        option.value = region;
                        option.text = region;
                        filterRegion.appendChild(option);
                    }
                    const sortedOptions = Array.from(valuesStatus).sort();
                    for (let i = 0; i < sortedOptions.length; i++) {
                        const option = document.createElement("option");
                        option.value = sortedOptions[i];
                        option.text = sortedOptions[i];
                        filterOwner.appendChild(option);
                    }
                    const filterTable = function () {
                        const selectedRegion = filterRegion.value;
                        const selectedStatus = filterOwner.value;
                        for (let i = 0; i < table.rows.length; i++) {
                            if (i === 0) {
                                table.rows[i].style.display = "";
                            } else {
                                const cells = table.rows[i].cells;
                                const region = cells[0].textContent;
                                const status = cells[2].textContent;
                                if ((selectedRegion === "" || selectedRegion === region) && (selectedStatus === "" || selectedStatus === status)) {
                                    table.rows[i].style.display = "";
                                } else {
                                    table.rows[i].style.display = "none";
                                }
                            }
                        }

                        // Hide the table if both drop-down lists are on their default values
                        if (selectedRegion === "" && selectedStatus === "") {
                            table.style.display = "none";
                        } else {
                            table.style.display = "";
                        }
                    };
                    filterRegion.addEventListener("change", filterTable);
                    filterOwner.addEventListener("change", filterTable);
                })
                .catch(error => console.error(error));
        };
    </script>
</body>
</html>
    This topic has been closed for replies.

    1 reply

    Legend
    March 4, 2023

    Is your table correctly formatted? A missing <td> tag or an incomplete closing tag somewhere within the table code will result in the information in the table still being visible - dont think the contents within the <table> tag will be hidden just because you have set display none if the table code is badly deformed.

     

    As an example below, if there is no opening <td> tag the information 'Testing' will display even though the table is set to display="none"

     

    <table id="csv-table">
    <tr>
    Testing</td>
    </tr>
    </table>

    <script>
    let table = document.getElementById("csv-table");
    let filterRegion = document.getElementById("filter-region");
    let filterOwner = document.getElementById("filter-owner");

    if(filterRegion.value === '' && filterOwner.value === '' ) {
    table.style.display = "none";
    }
    </script>

     

    Is it possible to post a few lines from the csv file here so I can test it? 

     

    I also see you are trying to place the table within a <div id="badcock_table"></div> and setting that to display none using css. That should work and conceal the table on page load.

     

    You could them make the <div> visible when the user selects an option from either the 'Region' or 'Owner/Manager' select menus by using an onchange event:

     

    <script>

    const badcockTable = document.getElementById("badcock_table");

     

    filterRegion.onchange = function() {

    badcockTable.style.display = "block";
    }

     

    filterOwner.onchange = function() {

    badcockTable.style.display = "block";
    }

    </script>