Reputation:
I have this very simple table. I'm trying to hide rows based on the value of the column HIST.
Basically:
I got stuck here but I'm not very far. Any help?
$('#historySelect').change(function() {
var option = $(this).val();
if (option === "No") {
} else {
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Show History
<select id="historySelect">
<option value="Yes">No</option>
<option value="No">Yes</option>
</select>
</label>
<table class="table dataTable no-footer" id="dataTable" cellspacing="0" width="100%" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
<thead>
<tr role="row">
<th class="text-center text-primary sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Name: activate to sort column ascending" style="width: 66px;">Name</th>
<th class="text-center text-primary hidden sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Historic: activate to sort column ascending" style="width: 0px;">Historic</th>
</tr>
</thead>
<tbody>
<tr class="text-center hover-table odd" role="row">
<td>Name</td>
<td class="hist hidden">true</td>
</tr>
<tr class="text-center hover-table odd" role="row">
<td>Name</td>
<td class="hist hidden">false</td>
</tr>
</tbody>
</table>
Upvotes: 2
Views: 62
Reputation: 7970
You can use :contains()
selector (it is case-sensative.).
$('#historySelect').change(function() {
var option = $(this).val();
if (option === "No") {
$(".hist:contains('false')").parent().hide();
$(".hist:contains('true')").parent().show();
} else {
$(".hist:contains('true')").parent().hide();
$(".hist:contains('false')").parent().show();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Show History
<select id="historySelect">
<option value="Yes">No</option>
<option value="No">Yes</option>
</select>
</label>
<table class="table dataTable no-footer" id="dataTable" cellspacing="0" width="100%" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
<thead>
<tr role="row">
<th class="text-center text-primary sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Name: activate to sort column ascending" style="width: 66px;">Name</th>
<th class="text-center text-primary hidden sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Historic: activate to sort column ascending" style="width: 0px;">Historic</th>
</tr>
</thead>
<tbody>
<tr class="text-center hover-table odd" role="row">
<td>Name</td>
<td class="hist hidden">true</td>
</tr>
<tr class="text-center hover-table odd" role="row">
<td>Name</td>
<td class="hist hidden">false</td>
</tr>
</tbody>
</table>
Upvotes: 0
Reputation: 337713
Firstly note that you have the value and text of the option
elements swapped.
To achieve what you require you can use has()
in conjunction with the :contains
selector to get the tr
elements that have a .hist
element that contains false
. Then you can show()
or hide()
as needed, like this:
$('#historySelect').change(function() {
var $rows = $('#dataTable tbody tr');
if ($(this).val() === "No") {
$rows.hide().has('.hist:contains(false)').show();
} else {
$rows.show();
}
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>Show History
<select id="historySelect">
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
</label>
<table class="table dataTable no-footer" id="dataTable" cellspacing="0" width="100%" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
<thead>
<tr role="row">
<th class="text-center text-primary sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Name: activate to sort column ascending" style="width: 66px;">Name</th>
<th class="text-center text-primary hidden sorting" tabindex="0" aria-controls="dataTable" rowspan="1" colspan="1" aria-label="Historic: activate to sort column ascending" style="width: 0px;">Historic</th>
</tr>
</thead>
<tbody>
<tr class="text-center hover-table odd" role="row">
<td>Name</td>
<td class="hist hidden">true</td>
</tr>
<tr class="text-center hover-table odd" role="row">
<td>Name</td>
<td class="hist hidden">false</td>
</tr>
</tbody>
</table>
Upvotes: 1