PDA

View Full Version : [CLOSED] Mathemetical Calculation between tow fields



matrixwebtech
Aug 27, 2014, 2:25 PM
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?


@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(DisplayFieldValu e) + AmountFieldValue));
}

if (parseFloat(newValue) < parseFloat(oldValue)) {

DisplayField.clear();
DisplayField.setValue((parseFloat(DisplayFieldValu e) - AmountFieldValue));
}
else {

DisplayField.clear();
HDFTotal.setValue((parseFloat(DisplayFieldValue) + AmountFieldValue));
DisplayField.setValue((parseFloat(DisplayFieldValu e) + 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,#{tx tsubtotal}, #{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,#{tx tsubtotal}, #{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)

geoffrey.mcgill
Aug 27, 2014, 2:59 PM
Can you try using .setRawValue() instead of .setValue().

http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.form.field.Text-method-setRawValue

matrixwebtech
Aug 27, 2014, 4:03 PM
Thanks for your comment.

setRawValue
helps to solve this.

Edit:
I just see after put value in "Tax 1%" when I press tab for go next field ,"txttax1r" field's


.Listeners(l =>
{
l.Change.Handler = "CalculatePercentage(#{txttax1r},newValue,oldValue, #{txtsubtotal}, #{txtTax1p}, #{lbl},#{hdf})";
})

fire again.

Daniil
Aug 28, 2014, 9:08 AM
Please try this:
http://forums.ext.net/showthread.php?30301&p=139091&viewfull=1#post139091