Pedro Oliveira
Pedro Oliveira

Reputation: 145

Filter First column of table Jquery

I'm trying to build a select filter using Jquery for a HTML table, and I want to filter only the first column of that table, so far I've got this function:

$('#select').change(function() {
    var rows = $("#tablebody_hosts").find("tr").hide();
    var data = this.value.split(" ");
    $.each(data, function(i, v) {
        rows.filter(":contains(" + v + ")").show();
    })
});

How can I make this just search for the data in the first column of the table?

Upvotes: 2

Views: 2316

Answers (3)

Rituraj ratan
Rituraj ratan

Reputation: 10388

$.each(data, function(i, v) {
  rows.filter(function(){
    return (data.indexOf($.trim($(this).children('td').first().text())) != -1);
 }).show();
 });

Upvotes: 0

user1636522
user1636522

Reputation:

Try this :

rows.filter(function () {
    return $(this).children(':eq(0)').text().indexOf(v) !== -1;
}).show();

Instead of :

rows.filter(":contains(" + v + ")").show();

Doc : http://api.jquery.com/eq-selector/.

Upvotes: 4

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

You don't need to iterate through the rows, Just try this,

rows.filter(function(){
    return (data.indexOf($.trim($(this).children('td').first().text())) != -1);
}).show();

Or

rows.filter(function(){
    return $.inArray($.trim($(this).children('td').first().text()),data);
}).show();

Upvotes: 0

Related Questions