Preethi Jain Preethi Jain - 7 months ago 11
Javascript Question

How to write a table filter in this case

I have got a table , and also a filter for Quantity column

My requirement is that , i want to display only the contents that are greater than the entered in the filter quanity column

This is my logic :

$(document).ready(function () {
(function ($) {
$('#filter').keyup(function () {
var rex = new RegExp($(this).val(), 'i');
$('#myTable tr').hide();
$('#myTable td:eq(2)').filter(function () {
return rex.test($(this).text());
}).show();
})
}(jQuery));
});


http://jsfiddle.net/52aK9/1578/

please let me know how to do this .

Answer

You can do it like following.

$('#filter').keyup(function() {
    var quantiy = +$(this).val(); //+ to convert string to number

    var tr = $('#myTable tbody tr');
    tr.hide();
    tr.filter(function() {
        return +$('td:eq(3)', this).text() > quantiy;  //select quantity column by '$('td:eq(3)', this)'
    }).show();
});

UPDATED FIDDLE