michaelmcgurk
michaelmcgurk

Reputation: 6509

Table - Live Search with jQuery

I have been working on a solution for a Live Search on my table of data.

When I search Jim it works as expected :-)

However, when I search Carey, no results appear. Why is this? :-(

Demo: http://jsfiddle.net/L1d7naem/

$("#search").on("keyup", function() {
    var value = $(this).val();

    $("table tr").each(function(index) {
        if (index !== 0) {

            $row = $(this);

            var id = $row.find("td:first").text();

            if (id.indexOf(value) !== 0) {
                $row.hide();
            }
            else {
                $row.show();
            }
        }
    });
});
table, tr, td, th{
    border: 1px solid blue;
    padding: 2px;
}

table th{
    background-color: #999999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><th>Forename</th><th>Surname</th><th>Extension</th></tr>
<tr><td>Jim</td><td>Carey</td><td>1945</td></tr>
<tr><td>Michael</td><td>Johnson</td><td>1946</td></tr>
</table>
<br />
<input type="text" id="search" placeholder="  live search"></input>

Upvotes: 2

Views: 1182

Answers (6)

Anoop LL
Anoop LL

Reputation: 1575

Hope this works,

$("#search").on("keyup", function() {
var value = $(this).val();

$("table tr").each(function(index) {
    if (index !== 0) {
        var id = $(this).children().text()
        if (id.indexOf(value) < 0) {
           $(this).hide();
        }else {
            $(this).show();
        }
    }
 });
});

Here is the working fiddle https://jsfiddle.net/44Lux7ze/

Upvotes: 0

Jekin Kalariya
Jekin Kalariya

Reputation: 3507

      $("#search").on("keyup", function() {
    var value = $(this).val();

    $("table tr").each(function(index) {
        if (index !== 0) {

            $row = $(this);

            var id = $.map($row.find('td'), function(element) {
    return $(element).text()
}).join(' ');





            if (id.indexOf(value) <0) {
                $row.hide();
            }
            else {
                $row.show();
            }
        }
    });
});

http://jsfiddle.net/Lyxex4tp/

Upvotes: 1

Ivan Sivak
Ivan Sivak

Reputation: 7488

Try this. You have to iterate through all columns and once you find any match just escape the loop using the return false; within each() function. Also, indexOf returns -1 if string is not found.

$("#search").on("keyup", function() {
    var value = $(this).val();

    $("table tr").each(function(index) {
        if (index !== 0) {

            $row = $(this);

            $row.find("td").each(function(){
              var id = $(this).text();
              if (id.indexOf(value) < 0) {
                $row.hide();
              }
              else {
                $row.show();
                return false;
              }
            });
        }
    });
});
table, tr, td, th{
    border: 1px solid blue;
    padding: 2px;
}

table th{
    background-color: #999999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr><th>Forename</th><th>Surname</th><th>Extension</th></tr>
<tr><td>Jim</td><td>Carey</td><td>1945</td></tr>
<tr><td>Michael</td><td>Johnson</td><td>1946</td></tr>
</table>
<br />
<input type="text" id="search" placeholder="  live search"></input>

Upvotes: 2

The behaviour you want can be achieved with the following corrections in the each loop (also note the < 0 in the condition...):

var id = $.map($row.find('td'), function(element) {
    return $(element).text()
}).join(' ');

if (id.indexOf(value) < 0) {
    $row.hide();
} else {
    $row.show();
}

Upvotes: 3

Abhishek Dhanraj Shahdeo
Abhishek Dhanraj Shahdeo

Reputation: 1356

Try this:

$("#search").on("keyup", function() {
var value = $(this).val();

$("table td").each(function() {
    if(value.match($(this).text)) {
        console.log($(this).text());
    }

    else {
        $("table").hide();
    }
});
});

Try matching with all the td elements.

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Its because of the following line:

var id = $row.find("td:first").text();  

You are working on the first column of table only and "Carey" is in second column of table

Upvotes: 6

Related Questions