简体   繁体   中英

How do I get my search function work to filter searched items from table?

I've made a table filled with data retreived from a JSON file. Now I'm trying to make a searchbar that filters searched items and only shows the table rows of the items searched for. The code of the function I'm using now is:

//Search function
          function searchTable() {
        var input, filter, found, table, tr, td, i, j;
        input = document.getElementsByClassName("searchBar");
        filter = input.value.toUpperCase();
        table = document.getElementById("productTable");
        tr = table.getElementsByTagName("tr");
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td");
            for (j = 0; j < td.length; j++) {
                if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
                    found = true;
                }
            }
            if (found) {
                tr[i].style.display = "";
                found = false;
            } else {
                tr[i].style.display = "none";
            }
        }
    }
});

This is the HTML of the table I'm trying to apply the filter to:

<input class="form-control searchBar" type="text" name="search" placeholder="search">
                    <table class="table table-hover">
                        <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Product Name</th>
                            <th scope="col">Free Stock</th>
                            <th scope="col">Price</th>
                            <th scope="col">Actions</th>
                        </tr>
                        </thead>
                        <tbody id="productTable">
                        <tr>
                        </tr>
                        </tbody>
                    </table>

Here is the best solution for searching inside HTML table while covering all of the table, (all td, tr in the table), pure javascript and as short as possible:

 <body style="background:red;"> <input id='myInput' onkeyup='searchTable()' type='text'> <table id='myTable'> <thead> <tr> <th scope="col">#</th> <th scope="col">Product Name</th> <th scope="col">Free Stock</th> <th scope="col">Price</th> <th scope="col">Actions</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Green</td> <td>Lorem</td> <td>Ipsum</td> <td>button</td> </tr> <tr> <td>2</td> <td>Green</td> <td>elit</td> <td>Mumbai</td> <td>button</td> </tr> <tr> <td>3</td> <td>Green</td> <td>sud</td> <td>Dummy</td> <td>button</td> </tr> </tbody> </table> <script> function searchTable() { var input, filter, found, table, tr, td, i, j; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); table = document.getElementById("myTable"); tr = table.getElementsByTagName("tr"); for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td"); for (j = 0; j < td.length; j++) { if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) { found = true; } } if (found) { tr[i].style.display = ""; found = false; } else { tr[i].style.display = "none"; } } } </script> </body>

The problem is getElementsByClassName does not return an element. Instead, it returns a NodeList. If you only have a single element with class=“searchBar” then you would reference as:

input = document.getElementsByClassName("searchBar")[0];

Updated script with your existing ID is

function searchTable() {
        var input, filter, found, table, tr, td, i, j;
        input = document.getElementsByClassName("searchBar")[0];
        filter = input.value.toUpperCase();
        table = document.getElementById("productTable");
        tr = table.getElementsByTagName("tr");
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td");
            for (j = 0; j < td.length; j++) {
                if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
                    found = true;
                }
            }
            if (found) {
                tr[i].style.display = "";
                found = false;
            } else {
                tr[i].style.display = "none";
            }
        }
    }

However, if you are only going to have one of these elements, I suggest giving it an id and use getElementById and then you will actually get an element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM