custom amount subtraction from more than one cell in html table
Clash Royale CLAN TAG#URR8PPP
custom amount subtraction from more than one cell in html table
$(".submitNum").click(function()
var $tr = $(this).closest('tr');
var $remaining = $tr.find(".remaining");
var $qty = $tr.find(".stockQuantity");
var $numToSubmit = $tr.find(".customInput");
//Get current values
var batchSize = $remaining.attr("data-maxNumber");
var currentRemaining = $remaining.text();
var currentQty = $qty.text();
var currentInputValue = $numToSubmit.val();
var difference = Number(currentRemaining) - Number(currentInputValue);
var divisible = Number(currentInputValue) / Number(batchSize);
var usesFromQuantity = Number(currentQty) * Number(batchSize);
var totalUses = Number(usesFromQuantity) + Number(currentRemaining);
var decimalPart = divisible - Math.round(divisible);
var finalRemaining = Number(decimalPart) * Number(batchSize);
//Subtract values
if (currentInputValue < 0)
alert('Please insert a valid quantity to withdraw');
else if (currentInputValue > totalUses)
alert("Cannot withdraw this amount");
else if (currentInputValue > Number(batchSize) + Number(currentRemaining) &&
Number(currentInputValue) < Number(totalUses))
currentQty = Number(currentQty) - Math.round(divisible);
currentRemaining = Math.round(finalRemaining);
else if (difference == 0)
currentRemaining = batchSize;
currentQty = currentQty - 1;
else if (difference < 0)
currentRemaining = Number(difference) + Number(batchSize);
currentQty = Number(currentQty) - 1;
else if (difference > 0)
currentRemaining = difference;
else if (currentInputValue == totalUses)
currentRemaining = 0;
currentQty = 0;
//Update text
$remaining.text(currentRemaining);
$qty.text(currentQty);
$tr.find(".collapseX").hide();
$tr.find(".inputBtn").show();
$tr.find('.customInput').val('');
);
ul li
display: inline;
body
text-align: center;
input
max-width: 100px;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Stock Quantity</th>
<th scope="col">Remaining Uses</th>
<th scope="col">Withdraw Item</th>
</tr>
</thead>
<tbody>
<tr>
<td>#Item1</td>
<td class="stockQuantity">3</td>
<td data-maxNumber="30" class="remaining">15</td>
<td>
<ul>
<li class="list-inline-item">
<input placeholder="Set uses qty" class="customInput" type="number" min="1">
</li>
<li class="list-inline-item"><button href="#" class=" submitNum">Sumit</button></li>
</ul>
</td>
</tr>
</tbody>
</table>
Okay so:
1 unit of stock quantity
has 30 x remaining uses
stock quantity
remaining uses
What I am confused in is creating an algorithm that calculates how much is left in the stock quantity
and the remaining uses
if the value of the input field is an amount that exceeds the current remaining uses
+ 30
which is the amount per 1 unit of stock quantity
so I can know how many units should I withdraw from stock quantity
and how many units should be remaining in remaining uses
.
stock quantity
remaining uses
remaining uses
30
stock quantity
stock quantity
remaining uses
The jquery function I have here is a bit big and cluttered, but its functional when I want to withdraw an amount that is less than or equal remaining uses + 30
.
remaining uses + 30
of course there can be smarter ways to do this feel free to change the whole thing
to understand what I mean better try withdrawing (45 units)
or less it'll work and then try to withdraw (70 units)
here you'll notice the bug.
(45 units)
(70 units)
1 Answer
1
You can use the modulus operator (%) to quickly figure out how many come out of remaining.
Say you have:
var qty = 3;
var remaining = 20;
var batchSize = 30;
var withdrawAttempt = 45;
Then:
var totalLeft = remaining + batchSize * qty;
if (withdrawAttempt > totalLeft)
// invalid
else
// calculate how many to take from remaining
var wr = withdrawAttempt % batchSize;
// update qty and remaining
qty -= (withdrawAttempt - wr) / batchSize;
remaining -= wr;
if (remaining < 0)
remaining += batchSize;
qty -= 1;
remaining
-10.
You’re right. Added a check for this case.
– James
Aug 12 at 23:23
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It works mainly, but if you with withdraw 45 two times in a row,
remaining
=-10.
– Ahmed ALgallad
Aug 12 at 22:46