Aug 27, 2014, 1:25 PM
[CLOSED] Mathemetical Calculation between tow fields
Situation :I have an order entry form, in this form some Tax entry fields are there.Tax can entry in two ways,
1.user can put Tax % ,then Tax amount will be calculated.
2. or User can put amount then Tax % will calculated.
and finally displayed in "Total" field .
for that I do:
Take value and after calculation calculated value to total field.
Problem: But problem is ,I use change handler and after calculation when another field value changed again that field's change event fires and calculation take place again.how can I prevent this?Is there any easy way to do this?
1.user can put Tax % ,then Tax amount will be calculated.
2. or User can put amount then Tax % will calculated.
and finally displayed in "Total" field .
for that I do:
Take value and after calculation calculated value to total field.
Problem: But problem is ,I use change handler and after calculation when another field value changed again that field's change event fires and calculation take place again.how can I prevent this?Is there any easy way to do this?
@using Ext.Net
@using Ext.Net.MVC
@{
Layout = null;
var X = Html.X();
}
<script type="text/javascript">
var CalculateAmount = function (PercentageField, newValue, oldValue, CalculationBasedOn, AmountField, DisplayField,HDFTotal,CalculationType) {
var calculationbasedon,
percentagefield,
amountfield,
displayfield
calculationbasedon = CalculationBasedOn.getValue();
percentagefield = PercentageField.getValue();
amountfield = AmountField.getValue();
displayfield = DisplayField.getValue();
DisplayField.clear();
AmountField.setValue((calculationbasedon * percentagefield) / 100)
DisplayTotal(DisplayField, AmountField.getValue(), newValue, oldValue, HDFTotal);
}
var CalculatePercentage = function (AmountField, newValue, oldValue, CalculationBasedOn, PercentageField, DisplayField, HDFTotal) {
var calculationbasedon,
percentagefield,
amountfield,
displayfield
calculationbasedon = CalculationBasedOn.getValue();
percentagefield = PercentageField.getValue();
amountfield = AmountField.getValue();
PercentageField.setValue((amountfield / calculationbasedon) * 100)
DisplayTotal(DisplayField, AmountField.getValue(), newValue, oldValue, HDFTotal);
}
var DisplayTotal = function (DisplayField, AmountFieldValue, newValue, oldValue, HDFTotal) {
try
{
var DisplayFieldValue = HDFTotal.getValue();
if (isNaN(DisplayFieldValue)) {
DisplayFieldValue = 0;
}
if(parseFloat(newValue)>parseFloat(oldValue))
{
DisplayField.clear();
DisplayField.setValue((parseFloat(DisplayFieldValue) + AmountFieldValue));
}
if (parseFloat(newValue) < parseFloat(oldValue)) {
DisplayField.clear();
DisplayField.setValue((parseFloat(DisplayFieldValue) - AmountFieldValue));
}
else {
DisplayField.clear();
HDFTotal.setValue((parseFloat(DisplayFieldValue) + AmountFieldValue));
DisplayField.setValue((parseFloat(DisplayFieldValue) + AmountFieldValue));
}
}
catch (ex) {
console.log(ex)
}
}
</script>
@X.ResourceManager()
@(X.TextField()
.ID("txtsubtotal")
.FieldLabel("Sub Total").Text("241")
)
@(X.Container().Layout(LayoutType.HBox)
.Items
(
X.TextField()
.ID("txtTax1p")
.Name("m")
.FieldLabel("Tax 1 %")
.EnableKeyEvents(true)
.Listeners(l =>
{
l.Change.Handler = "CalculateAmount(#{txtTax1p},newValue,oldValue,#{txtsubtotal}, #{txttax1r}, #{lbl},#{hdf} )";
})
,
X.NumberField()
.ID("txttax1r")
.HideTrigger(true)
.Name("m")
.FieldLabel("Tax 1 rate")
.EnableKeyEvents(true)
.Listeners(l =>
{
l.Change.Handler = "CalculatePercentage(#{txttax1r},newValue,oldValue,#{txtsubtotal}, #{txtTax1p}, #{lbl},#{hdf})";
})
)
)
@(X.Container().Layout(LayoutType.HBox)
.Items
(
X.NumberField()
.ID("txtTax2p")
.Name("m")
.HideTrigger(true)
.FieldLabel("Tax 2 %")
.EnableKeyEvents(true)
.Listeners(l =>
{
l.Change.Handler = "CalculateAmount(#{txtTax2p},newValue,oldValue,#{txtsubtotal}, #{txttax2r}, #{lbl},#{hdf} )";
})
,
X.NumberField()
.ID("txttax2r")
.HideTrigger(true)
.FieldLabel("Tax 2 rate")
.Name("m")
.EnableKeyEvents(true)
.Listeners(l =>
{
l.Change.Handler = "CalculatePercentage(#{txttax2r},newValue,oldValue,#{txtsubtotal}, #{txtTax2p}, #{lbl},#{hdf})";
})
)
)
@X.DisplayField().ID("lbl").Text("0").FieldLabel("Total")
@X.Hidden().ID("hdf").Value(0)
Last edited by matrixwebtech; Nov 05, 2014 at 12:06 PM.