Task
I have a table with multiple columns. Each column has an input field above it. I want to filter each row such that the table shows any rows that have at least one column that contains the value in the input field above the column.
Issue
When I try typing in values into the input fields, the table does not filter. I am not sure where my error is. Since I am still very much in the learning phase, any tips for syntax or performance would also be appreciated. The following minimally (not-yet) working example is the best I could do based on various answers on this website.
Code
function performReset() {
document.getElementById("inputName").value = "";
document.getElementById("inputCity").value = "";
document.getElementById("inputCountry").value = "";
filterTable();
}
function filterTable() {
// Get the table rows.
let rows = document.querySelector("#myTable > tbody").rows;
// Get the inputed values to filter against.
let inputedName = document.getElementById("inputName").value.toUpperCase();
let inputedCity = document.getElementById("inputCity").value.toUpperCase();
let inputedCountry = document.getElementById("inputCountry").value.toUpperCase();
// Loop over the rows to perform the filter.
let totalRows = rows.length;
for (let i = 0; i < totalRows; i++) {
// Get the contents of the relevant cell data.
let rowName = rows[i].cells[0].textContent.toUpperCase();
let rowCity = rows[i].cells[1].textContent.toUpperCase();
let rowCountry = rows[i].cells[2].textContent.toUpperCase();
// Define an array of conditions.
// Note: the conditions are evaluated here.
let conditionsArray = [
rowName.indexOf(inputedName) > -1,
rowCity.indexOf(inputedCity) > -1,
rowCountry.indexOf(inputedCountry) > -1
];
// If any of the above conditions are true then show the row,
// otherwise turn of displaying that row.
if (conditionsArray.indexOf(true) > -1) {
rows[i].display = "";
} else {
rows[i].display = "none";
}
}
}
<link href="https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet" />
<h2 class="w3-center">Filter Table Test</h2>
<p class="w3-center"><button id="buttonReset" onclick="performReset()" class="w3-button w3-grey w3-hover-light-grey">Reset</button></p>
<table id="myTable" class="w3-table w3-striped w3-hoverable">
<!--Inputs Used For Filtering-->
<tr>
<td><input id="inputName" onkeyup="filterTable()" class="w3-input" placeholder="Name..."></td>
<td><input id="inputCity" onkeyup="filterTable()" class="w3-input" placeholder="City..."></td>
<td><input id="inputCountry" onkeyup="filterTable()" class="w3-input" placeholder="Country..."></td>
</tr>
<!--Column Headings-->
<tr class="w3-dark-grey">
<th>Customer</th>
<th>City</th>
<th>Country</th>
</tr>
<!--Row Data-->
<tbody>
<tr class="item">
<td>Alfreds Futterkiste</td>
<td>Berlin</td>
<td>Germany</td>
</tr>
<tr class="item">
<td>Berglunds snabbköp</td>
<td>Lule </td>
<td>Sweden</td>
</tr>
<tr class="item">
<td>Centro comercial Moctezuma</td>
<td>México D.F.</td>
<td>Mexico</td>
</tr>
<tr class="item">
<td>Ernst Handel</td>
<td>Graz</td>
<td>Austria</td>
</tr>
<tr class="item">
<td>FISSA Fabrica Inter. Salchichas S.A.</td>
<td>Madrid</td>
<td>Spain</td>
</tr>
<tr class="item">
<td>Galería del gastrónomo</td>
<td>Barcelona</td>
<td>Spain</td>
</tr>
<tr class="item">
<td>Island Trading</td>
<td>Cowes</td>
<td>UK</td>
</tr>
<tr class="item">
<td>Königlich Essen</td>
<td>Brandenburg</td>
<td>Germany</td>
</tr>
<tr class="item">
<td>Laughing Bacchus Wine Cellars</td>
<td>Vancouver</td>
<td>Canada</td>
</tr>
<tr class="item">
<td>Magazzini Alimentari Riuniti</td>
<td>Bergamo</td>
<td>Italy</td>
</tr>
<tr class="item">
<td>North/South</td>
<td>London</td>
<td>UK</td>
</tr>
<tr class="item">
<td>Paris spécialités</td>
<td>Paris</td>
<td>France</td>
</tr>
<tr class="item">
<td>Rattlesnake Canyon Grocery</td>
<td>Albuquerque</td>
<td>USA</td>
</tr>
<tr class="item">
<td>Simons bistro</td>
<td>København</td>
<td>Denmark</td>
</tr>
<tr class="item">
<td>The Big Cheese</td>
<td>Portland</td>
<td>USA</td>
</tr>
<tr class="item">
<td>Vaffeljernet</td>
<td>Århus</td>
<td>Denmark</td>
</tr>
<tr class="item">
<td>Wolski Zajazd</td>
<td>Warszawa</td>
<td>Poland</td>
</tr>
</tbody>
</table>
Edit 1: Modified document.querySelector('#myTable tbody') to document.querySelector('#myTable > tbody'). But my issue persists.
"#myTable > tbody"note the greater than sign. Currently, the selector is only selecting the two TRs that are prior to thetbody.