randomizertech
randomizertech

Reputation: 2329

How to make a filter look for info in only one column with jQuery?

I am using the following code to filter data in my table:

function searchFilter(ftr,table){
    ftr = '#'+ftr;
    table = '#'+table;
    var $rows = $(table+' tbody tr');
    $(ftr).change(function() {
        var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

        $rows.show().filter(function() {
        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
        return !~text.indexOf(val);
        }).hide();
    });

}

But if there is information repeated in other columns it will show them too. How can I be specific on what column I want the filter to work? Let's say the id of this column is betaDate. What am I missing here?

Upvotes: 0

Views: 120

Answers (1)

epascarello
epascarello

Reputation: 207537

Well you would get the index of the column

var searchIndex = $("#betaDate").index();

and in the filter row you can use the index

$(this).find("td").eq(searchIndex).text()...

And another way of doing it [logic might be off, but basic idea]

var index = 1; //starts at one not zero!
var text = "1";
$("table tbody tr td:nth-child(" + index + ")")
    .filter( function() {
        return $(this).text()!==text; 
     })
     .parent()
         .addClass("hide");

fiddle

Upvotes: 2

Related Questions