I am trying to hide specific table rows using checkboxes as filters to hide the rows. I have searched tons of different examples for checkboxes, css visibility, jquery, etc, etc. However, I have not found any examples that fit my scenario, and I can't seem to hack together a custom solution based on examples I have found.
- Checking a checkbox should REMOVE ANY ROWS that DO NOT match checkbox value. For example, if I check the "Male" checkbox, it should HIDE the rows which DO NOT have the "gender" value of "male"
- Unchecking the checkbox should make corresponding rows visible again
- Code should be able to handle multiple checkboxes. For example if I check the "Droid" checkbox AND I check the "Male" checkbox, NO RESULTS should display, because NO RESULTS exist that MATCH BOTH conditions.
UPDATE:
I screwed up the initial logic.
- If NO checkboxes are checked, SHOW EVERYTHING.
- If a checkbox is checked, show the corresponding rows for that checkbox. Iterate any checkboxes that are checked, and show corresponding rows. Hide any rows that are NOT checked.
- If NO checkboxes are checked, SHOW EVERYTHING.
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 8px; }
tr:nth-child(even){background-color: #f2f2f2}
th { background-color: #0099ff; color: white; }
table, th, td { border: 1px solid black; }
* { box-sizing: border-box; }
body { margin: 0; }
.column {
float: left;
padding: 10px;
height: 300px; /* Should be removed. Only for demonstration */
}
.left { width: 75%; }
.right { width: 25%; }
.row:after {
content: "";
display: table;
clear: both;
}
<div class="column left">
<table>
<thead>
<th>Name</th>
<th>Occupation</th>
<th>Gender</th>
<th>Race</th>
</thead>
<tbody>
<tr gender="male" race="sith">
<td>Darth Vader</td>
<td>Dark Lord of the Sith</td>
<td>Male</td>
<td>Sith</td>
</tr>
<tr gender="male" race="human">
<td>Boba Fett</td>
<td>Bounty Hunter</td>
<td>Male</td>
<td>Human</td>
</tr>
<tr gender="male" race="unknown">
<td>Yoda</td>
<td>Jedi Master</td>
<td>Male</td>
<td>Unknown</td>
</tr>
<tr gender="non-binary" race="droid">
<td>R2D2</td>
<td>Astromech Droid</td>
<td>Non-Binary</td>
<td>Droid</td>
</tr>
</tbody>
</table>
</div>
<div class="column right">
<div><b>Filters</b></div>
<div><b>Gender</b></div>
<div><input type="checkbox" value="male">Male</input></div>
<div><input type="checkbox" value="non-binary">Non-Binary</input></div>
<div><b>Race</b></div>
<div><input type="checkbox" value="sith">Sith</input></div>
<div><input type="checkbox" value="human">Human</input></div>
<div><input type="checkbox" value="unknown">Unknown</input></div>
<div><input type="checkbox" value="droid">Droid</input></div>
</div>