I have a table like this in my page:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<input type="text" id="myInput" placeholder="Search for anything.." title="Type in a name" ></input>
<table class="table table-bordered table-striped" >
<tbody id="myTable">
<td>
<?php echo $product_name ?>
</td>
<td>
<?php echo $gender ?>
</td>
<td>
<?php echo $date ?>
</td>
</td>
<td>
<?php echo $quantity ?>
</td>
<td>
<?php echo $shopname ?>
</td>
<td class="valor text-right">
<?php echo $price ?>
</td>
</tbody>
<tfoot>
<tr class="info">
<td colspan="2" class="text-right">TOTAL:</td>
<td class="total text-right"></td>
</tr>
</tfoot>
</table>
The idea here is to calculate the total for the column with class valor and it's shown on foot of the table at the colmn with class total.
And also the idea is that the user can filter rows typing anything on the input text named myInput and therefore the total should be recalculated.
By this time I'm able to filter rows with the jQuery code below:
$(document).ready(function () {
var $rows = $(".table tbody tr");
var total = 0;
$rows.each(function () {
total += parseFloat($(this).find(".valor").text().replace(/\./g, "").replace(",", "."));
});
$("#myInput").keyup(function () {
var filtertext = $(this).val();
var regex = new RegExp(filtertext, 'i');
$rows.hide().filter(function () {
return regex.test($(this).text());
}).show();
$(".table tbody tr:visible").each(function () {
total += parseFloat($(this).find(".valor").text().replace(/\./g, "").replace(",", "."));
});
$(".total").html(formataTotal(total.toFixed(2)));
});
$(".total").html(formataTotal(total.toFixed(2)));
});
function formatTotal(num) {
var parts = num.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ".");
return parts.join(",");
}
By using the above code m successfully filtering the rows but i am not getting any sum of the column after being filtered rows nor the total sum of that column valor