P
pepe
Hello,
I have a table with 3 columns I'll call columns A, B and C. In column
A I have denominations (bill names such as $1, $2, $5, etc.). In
column B I enter the number of bills I have (not the $ amount but the
count of bills). Column C receives the calculated amount by a
Javascript function called 'updateDenominationAmounts'.
Examples:
Column A: $1
Column B: 5
Column C: $5.00
Column A: $5
Column B: 2
Column C $10.00
My original table has 'denominations' from 1 cent up to $100 but the
example below only has 2 rows, for $1 and $5 bills.
The problem we are seeing is that when values are entered very rapidly
by, for example, entering a 1 in all fields (1 - tab - 1 - tab...)
during testing sometimes some of the calculated fields in column C
don't get updated. This problem does never occur if the pace of
entering data is 'normal'.
The following is the full code of the Javascript function being
invoked (I am not a Javascript expert so I'm sure there might be
better ways of making this work) and a sample of the fields in the
table involved in the process.
Any idea why this might be happening?
Thank you very much.
<script type="text/javascript">
function updateDenominationAmounts(quantity_element, denomination_id,
denomination_value, denomination_multiplier) {
var precision = denomination_multiplier.toString().length -
denomination_multiplier.toString().lastIndexOf('.') - 1;
var initial_value = parseFloat($('denomination_id_' +
denomination_id + '_amount').value);
var value = quantity_element.value * denomination_value *
denomination_multiplier;
if (isNaN(value)) {
value = 0;
}
$('denomination_id_' + denomination_id + '_amount').value =
parseFloat(value).toFixed(precision);
new Effect.Highlight('denomination_id_' + denomination_id +
'_amount', {
startcolor: "#FFFFFF",
endcolor: "#5E918D",
restorecolor: "#FFFFFF"
});
$('denominations_amount').value =
(
parseFloat($('denominations_amount').value) -
parseFloat(initial_value) +
parseFloat($('denomination_id_' + denomination_id +
'_amount').value)
).toFixed(precision);
new Effect.Highlight('denominations_amount', {
startcolor: "#FFFFFF",
endcolor: "#5E918D",
restorecolor: "#FFFFFF"
});
} // function updateDenominationAmounts()
</script>
<table>
<tr>
<th>Denomination</th>
<th>Count</th>
<th>Amount</th>
</tr>
<tr>
<td>1</td>
<td>
<input class="number"
id="bank_bank_denominations_attributes_0_quantity"
name="bank[bank_denominations_attributes][0][quantity]"
onkeyup="updateDenominationAmounts(this, 1, 1, 0.01);" size="30"
type="text" />
</td>
<td>
<input class="number" disabled="disabled"
id="denomination_id_1_amount" name="denomination_id_1_amount"
type="text" value="0.00" />
</td>
</tr>
<tr>
<td>$5</td>
<td>
<input class="number"
id="bank_bank_denominations_attributes_1_quantity"
name="bank[bank_denominations_attributes][1][quantity]"
onkeyup="updateDenominationAmounts(this, 2, 5, 0.01);" size="30"
type="text" />
</td>
<td>
<input class="number" disabled="disabled"
id="denomination_id_2_amount" name="denomination_id_2_amount"
type="text" value="0.00" />
</td>
</tr>
<tr>
<th>Total</th>
<th></th>
<th><input class="number" id="denominations_amount"
name="denominations_amount" readonly="readonly" type="text"
value="0.00" /></th>
</tr>
</table>
I have a table with 3 columns I'll call columns A, B and C. In column
A I have denominations (bill names such as $1, $2, $5, etc.). In
column B I enter the number of bills I have (not the $ amount but the
count of bills). Column C receives the calculated amount by a
Javascript function called 'updateDenominationAmounts'.
Examples:
Column A: $1
Column B: 5
Column C: $5.00
Column A: $5
Column B: 2
Column C $10.00
My original table has 'denominations' from 1 cent up to $100 but the
example below only has 2 rows, for $1 and $5 bills.
The problem we are seeing is that when values are entered very rapidly
by, for example, entering a 1 in all fields (1 - tab - 1 - tab...)
during testing sometimes some of the calculated fields in column C
don't get updated. This problem does never occur if the pace of
entering data is 'normal'.
The following is the full code of the Javascript function being
invoked (I am not a Javascript expert so I'm sure there might be
better ways of making this work) and a sample of the fields in the
table involved in the process.
Any idea why this might be happening?
Thank you very much.
<script type="text/javascript">
function updateDenominationAmounts(quantity_element, denomination_id,
denomination_value, denomination_multiplier) {
var precision = denomination_multiplier.toString().length -
denomination_multiplier.toString().lastIndexOf('.') - 1;
var initial_value = parseFloat($('denomination_id_' +
denomination_id + '_amount').value);
var value = quantity_element.value * denomination_value *
denomination_multiplier;
if (isNaN(value)) {
value = 0;
}
$('denomination_id_' + denomination_id + '_amount').value =
parseFloat(value).toFixed(precision);
new Effect.Highlight('denomination_id_' + denomination_id +
'_amount', {
startcolor: "#FFFFFF",
endcolor: "#5E918D",
restorecolor: "#FFFFFF"
});
$('denominations_amount').value =
(
parseFloat($('denominations_amount').value) -
parseFloat(initial_value) +
parseFloat($('denomination_id_' + denomination_id +
'_amount').value)
).toFixed(precision);
new Effect.Highlight('denominations_amount', {
startcolor: "#FFFFFF",
endcolor: "#5E918D",
restorecolor: "#FFFFFF"
});
} // function updateDenominationAmounts()
</script>
<table>
<tr>
<th>Denomination</th>
<th>Count</th>
<th>Amount</th>
</tr>
<tr>
<td>1</td>
<td>
<input class="number"
id="bank_bank_denominations_attributes_0_quantity"
name="bank[bank_denominations_attributes][0][quantity]"
onkeyup="updateDenominationAmounts(this, 1, 1, 0.01);" size="30"
type="text" />
</td>
<td>
<input class="number" disabled="disabled"
id="denomination_id_1_amount" name="denomination_id_1_amount"
type="text" value="0.00" />
</td>
</tr>
<tr>
<td>$5</td>
<td>
<input class="number"
id="bank_bank_denominations_attributes_1_quantity"
name="bank[bank_denominations_attributes][1][quantity]"
onkeyup="updateDenominationAmounts(this, 2, 5, 0.01);" size="30"
type="text" />
</td>
<td>
<input class="number" disabled="disabled"
id="denomination_id_2_amount" name="denomination_id_2_amount"
type="text" value="0.00" />
</td>
</tr>
<tr>
<th>Total</th>
<th></th>
<th><input class="number" id="denominations_amount"
name="denominations_amount" readonly="readonly" type="text"
value="0.00" /></th>
</tr>
</table>