Bootstrap Table How Hidden, When Toggled Visible Using Javascript It Does Not Show Correctly
I have a bootstrap table. I am using css to hide one of the table rows initially. I have a checkbox that I want to show the hidden table row if it is selected, otherwise it should
Solution 1:
I made a jsfiddle: https://jsfiddle.net/ov7d9rLa/
I think the problem might have been due to fighting between using bootstrap and CSS for hiding/showing. I simplified this by using the bootstrap class "hidden", and toggling that class with the click event.
<div class="orderTotal">
<h4>Order Total:</h4>
<table id="tableOrderTotal" class="table tableTotal">
<tbody>
<tr>
<td>Item1</td>
<td class="price-1">50</td>
</tr>
<tr id='hiddenRow' class="hidden">
<td>Item2</td>
<td class="price-2">13</td>
</tr>
<tr>
<td>Item3</td>
<td class="price-3">30</td>
</tr>
<tr class="summary">
<td class="totalOrder">Total:</td>
<td id="result" class="totalAmount"></td>
</tr>
</tbody>
</table>
</div>
<button id='toggle' class='btn btn-primary'>
Toggle
</button>
JS:
$("#toggle").on("click", function() {
$("#hiddenRow").toggleClass("hidden");
});
Solution 2:
I wouldn't use all of that css. jQuery makes it 100x's cleaner in my opinion. Here's what I would do:
I'd take advantage of jQuery's hide()
and toggle()
. Something like this:
$(document).ready(function() {
// Hide the row when the page loads
$("#hiddenRow").hide();
// when the user clicks the checkbox, toggle the row
$("#toggleCheck").click(function() {
$("#hiddenRow").toggle();
})
});
Here's a full JSBin: http://jsbin.com/nokusunamo/edit?html,js,console,output
Solution 3:
Just use display: table-row
as suggested @Louys in above comment :
#hiddenRow.show{
display: table-row;
}
Hope this helps.
$("input[name='product_CD']").on("click", function() {
$("#hiddenRow").toggleClass('show');
});
#hiddenRow {
display: none;
}
.orderTotal {
padding: 10px;
background-color: #fdfbe4;
width: 95%;
margin: 0 auto;
}
.orderTotal h4 {
font-weight: bold;
}
.totalOrder {
color: #ee7a23;
font-weight: bold;
font-size: 18px;
}
.totalAmount {
font-weight: bold;
font-size: 18px;
}
#hiddenRow.show{
display: table-row;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name='product_CD' type="checkbox" />
<div class="orderTotal">
<h4>Order Total:</h4>
<table id="tableOrderTotal" class="table tableTotal">
<tbody>
<tr>
<td>Item1</td>
<td class="price-1">50</td>
</tr>
<tr id="hiddenRow">
<td>Item2</td>
<td class="price-2">13</td>
</tr>
<tr>
<td>Item3</td>
<td class="price-3">30</td>
</tr>
<tr class="summary">
<td class="totalOrder">Total:</td>
<td id="result" class="totalAmount"></td>
</tr>
</tbody>
</table>
</div>
Post a Comment for "Bootstrap Table How Hidden, When Toggled Visible Using Javascript It Does Not Show Correctly"