Search code examples
javascriptjqueryhtml-tabledropdown

How to add a jQuery filter to a html table?


I'm trying to put a drop down filter to a html table using JQuery. Here's the code.

report.php

<table id ="myTable" class="table table-striped">

            <thead>
                <tr>
                    <th></th>
                    <th> First Name </th>
                    <th> Last Name </th>                  
                    <th> Phone </th>
                    <th> Email </th>              
                    <th> Gender</th>

                        <th>Term
                        <select id="filterText"  onchange='filterText()'>
                              <option disabled selected>Select</option>
                              <option value="all">All</option>
                              <option value="Fall2018">Fall2018</option> 
                              <option value="Srping2019">Spring2019</option>
                        </select></th>
                    <th> Action </th>

                </tr>
            </thead> 
            <?php
            if (count($report) === 0) {
                echo "<tr>No Reports</tr>";
            } else {

                for ($i = 0; $i < count($report); $i++) {

                    echo

                    "<tr class='row'>

                        <td>{$report[$i]['FName']}</td>
                        <td>{$report[$i]['LName']}</td>
                        <td>{$report[$i]['HPhone']}</td>
                        <td>{$report[$i]['PEmail']}</td>
                        <td>{$report[$i]['Gender']}</td>
                        <td>{$report[$i]['Term']}</td>

                        <td><a class=btn href='read.php?id=".$report[$i]['RegId']."'>Read</a></td>  

                    </tr>";

                }
            }



            ?>
        </table>

main.js

function filterText()
    {  
        var rex = new RegExp($('#filterText').val());
        if(rex ==="/all/"){clearFilter();}else{
            $('.row').show();
            $('.row').filter(function() {
            return rex.test($(this).text());
            }).hide();
    }
    }

   function clearFilter()
    {
        $('.filterText').val('');
        $('.row').show();
    }

I'm adding the dropdown filter to the term column. This code gives the opposite results. Like when I click on the 'All' option of the dropdown, it shows Spring2019 in the results. And when I click on the 'Spring2019' it shows all the values. And 'Fall2018' also shows all the Spring2019 values.

Can you check what is wrong in the code?


Solution

  • Salam, i think you can filter by cell text instead of row text, just add class to your cell

    <td>{$report[$i]['Term']}</td>
    

    like that

    <td class='term'>{$report[$i]['Term']}</td>
    

    and change your search function to

    function filterText()
        {  
            var val = $('#filterText').val().toLowerCase();
            if(val === "")
               return;
            if(val === "all")
              clearFilter();
            else
            $('.term').each(function() {
                $(this).parent().toggle($(this).text().toLowerCase() === val);
                });
    
        }