I am trying to apply multiple filtering using two input tags.
The first one asks for the employee id and the second one asks for the employee name. The table is reading data from a .xlsx file.
Here is my code for filtering.
$rows = $("#tblData tr");
$("#empId", "#empName").on("input", function () {
var val1 = $.trim($('#empId').val()).replace(/ +/g, ' ').toLowerCase();
var val2 = $.trim($('#empName').val()).replace(/ +/g, ' ').toLowerCase();
$rows.show().filter(function () {
var text1 = $(this).find("td:nth-child(1)").text().replace(/\s+/g, ' ').toLowerCase();
var text2 = $(this).find("td:nth-child(2)").text().replace(/\s+/g, ' ').toLowerCase();
return !~text1.indexOf(val1) || !~text2.indexOf(val2);
}).hide();
});
I heard that I should change the statements td:nth-child(1) and td:nth-child(2) to the index of the row rather than hardcoding it.
$(this).find("td").eq(myVariable).text().etchetera..wheremyVariableis a zero-based integer. There is only two here, but you could loop. -- But do you actually look for advices or are you stucked on a specific bug? If the code works, and advices are wished, look for Code Review.