Pawan
Pawan

Reputation: 32321

How to make table search by only one column in this code

As User types in the input i am searching the table and displaying results accordingly .

Currently there are two td's in the table Name and Course

Is it Possible to make the search only for Name , but not Course

At present this works for both (Name and Course )

This is my code

$(document).ready(function()
{
        $('#searchinputtext').keyup(function()
        {
          var tr = $('#videosfromtagstable tbody tr'); //use tr not td
           if ($(this).val().length >= 2) {
                var inputdata = $.trim($("#searchinputtext").val());
                $('#errmsgnovideos').hide();
                var noElemvideo = true;
                var val = $.trim(this.value).toLowerCase();

                el = tr.filter(function()
                {
                        return this.innerHTML.toLowerCase().indexOf(val) >= 0;
                }); // <==== closest("tr") removed
                if (el.length >= 1)
                {
                        noElemvideo = false;
                }
                //now you fadeIn/Out every row not every cell
                tr.not(el).fadeOut();
                el.fadeIn();
                if (noElemvideo)
                        if (inputdata !== '')
                        {
                                $('#errmsgnovideos').html('No Results Matched').show();
                        }
                        else
                        {
                                $('#errmsgnovideos').hide();
                        }
                        }
                        else {
      tr.fadeIn(); //show all if length does not match the required number of characters
      $('#errmsgnovideos').hide();
    }

        })
});

http://jsfiddle.net/e08o7uct/30/

Upvotes: 0

Views: 246

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

To achieve this you just need to change your filter() logic so that it only looks at the text of the first td within the given tr. To do that you can use :eq(), like this:

el = tr.filter(function() {
    return $(this).find('td:eq(0)').text().toLowerCase().indexOf(val) >= 0;
}); 

Updated fiddle

Upvotes: 1

Related Questions