How To Create A Filterable Table Using JQuery With Example

admin_img Posted By Bajarangi soft , Posted On 16-10-2020

In Jquery we can filter or search for specific elements in table .So today we discuss how to do it

How To Create A Filterable Table Using JQuery With Example

Step 1:Create index.html to impelement below code.

 

<input id="myInput" type="text" placeholder="Search..">
<br><br>

<table class="table">
    <thead>
    <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody id="myTable">
    <tr>
        <td>Sheetal</td>
        <td>Doe</td>
        <td>Sheetal@example.com</td>
    </tr>
    <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>Mary@example.com</td>
    </tr>
    <tr>
        <td>Marlin</td>
        <td>Dooley</td>
        <td>Marlin@example.com</td>
    </tr>
    <tr>
        <td>Anja</td>
        <td>Ravendale</td>
        <td>Anja@example.com</td>
    </tr>
    </tbody>
</table>


Step 2:Now impement jquery to serach specified element.
 
<script>
    $(document).ready(function(){
        $("#myInput").on("keyup", function() {
            var value = $(this).val().toLowerCase();
            $("#myTable tr").filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });
</script>


Complete Code For  Creating A Filterable Table Using JQuery 
 
<!DOCTYPE html>
<html>
<head>
    <title>How To Create A Filterable Table Using JQuery With Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<style>
    body {
        background: black;
    }
</style>
<body>
<div class="container">
    <br><br><br><br>
    <div class="text-center">
        <h1 id="color" style="color: White;">Create A Filterable Table Using JQuery</h1>
    </div>
    <br>

    <div class="well">
        <input id="myInput" type="text" placeholder="Search..">
        <br><br>

        <table class="table">
            <thead>
            <tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Email</th>
            </tr>
            </thead>
            <tbody id="myTable">
            <tr>
                <td>Sheetal</td>
                <td>Doe</td>
                <td>Sheetal@example.com</td>
            </tr>
            <tr>
                <td>Mary</td>
                <td>Moe</td>
                <td>Mary@example.com</td>
            </tr>
            <tr>
                <td>Marlin</td>
                <td>Dooley</td>
                <td>Marlin@example.com</td>
            </tr>
            <tr>
                <td>Anja</td>
                <td>Ravendale</td>
                <td>Anja@example.com</td>
            </tr>
            </tbody>
        </table>
    </div>

</div>
</body>
</html>
<script>
    $(document).ready(function(){
        $("#myInput").on("keyup", function() {
            var value = $(this).val().toLowerCase();
            $("#myTable tr").filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });
</script>

Related Post