I need to add back the pay amount to the outstanding amount if the row of the checkbox is unchecked. Currently what am I facing now is when I unchecked, the outstanding amount keep decreasing and did not add back. My second row did not have pay amount but if I checked it will also keep decreasing the value. Can someone help with my coding?
Initial version
Name | Outstanding | Pay |
Item 1| 20,000 | 0.00 |
Item 2| 10,000 | 0.00 |
Item 3| 3,000 | 0.00 |
My expected result for checked:
Name | Outstanding | Pay | |
Item 1| 14,000 | 6,000 | checked |
Item 2| 7,000 | 3,000 | checked |
Item 3| 3,000 | 0.00 | |
My expected result for unchecked:
If unchecked, it will add back the amount to outstanding.
Name | Outstanding | Pay | |
Item 1| 20,000 | 6,000 | unchecked|
Item 2| 7,000 | 3,000 | checked |
Item 3| 3,000 | 0.00 | |
Issue that I faced:
- I input
6,000in first row and checked, the outstanding will become14,000. Then I checked the second row without input pay amount, the second row outstanding amount will also deduct and deduct again for first row. But it should not deduct again or deduct the amount since the pay amount is zero. - If I keep unchecked and checked, the amount will keep decrease.
Name | Outstanding | Pay | |
Item 1| 8,000 | 6,000 | checked |
Item 2| 18,000 | 0.00 | checked |
Item 3| 3,000 | 0.00 | |
$(function() {
function calcSum(t) {
var result = 0.00;
var OutAmt = 0.00;
var Amt = 0.00;
$("tbody tr").each(function(i, r) {
if ($('input[name="chck"]', r).is(":checked")) {
result += parseFloat($(".payAmt", r).val());
OutAmt += parseFloat($(".outstandingAmt", r).text().replace(/,/g, ''))
Amt = $('.outstandingAmt', r).text((OutAmt - result).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'));
}
});
return result;
}
function updateSum(tbl) {
var t = calcSum(tbl);
$("#txtUnappliedAmt").val(t.toFixed(2));
}
updateSum($("#price-list"));
$("#price-list input").change(function() {
updateSum($("#price-list"));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="price-list">
<thead>
<tr>
<th>Name</th>
<th>Outstanding</th>
<th>Pay</th>
</tr>
</thead>
<tbody>
<tr>
<td class="item name">Item 1</td>
<td class="outstandingAmt">20,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
<tr>
<td class="item name">Item 2</td>
<td class="outstandingAmt">10,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
<tr>
<td class="item name">Item 3</td>
<td class="outstandingAmt">5,000.00</td>
<td class="item price"><input class="payAmt" type="text" value="0.00"></td>
<td><input type="checkbox" name="chck"></td>
</tr>
</tbody>
</table>
<div>
<label><b>Unapplied Amount:</b></label>
<input type="text" id="txtUnappliedAmt" value="0.00" disabled>
</div>