[CLOSED] Mathemetical Calculation between tow fields

  1. #1

    [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?

    @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.
  2. #2
    Can you try using .setRawValue() instead of .setValue().

    http://docs.sencha.com/extjs/4.2.1/#...od-setRawValue
    Geoffrey McGill
    Founder
  3. #3
    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.
    Last edited by matrixwebtech; Nov 05, 2014 at 12:06 PM.
  4. #4

Similar Threads

  1. UserControl don't get values of fields.
    By flaviodamaia in forum 2.x Help
    Replies: 1
    Last Post: Dec 02, 2013, 3:47 PM
  2. [CLOSED] Disabled fields don't get submitted
    By zwf in forum 2.x Legacy Premium Help
    Replies: 7
    Last Post: Nov 01, 2012, 11:47 AM
  3. Dynamic fields
    By Wtower in forum 1.x Help
    Replies: 2
    Last Post: Apr 20, 2009, 5:57 PM
  4. Fields Money
    By Maia in forum 1.x Help
    Replies: 2
    Last Post: Mar 25, 2009, 7:46 PM
  5. Reset all fields
    By james.healey in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jan 11, 2009, 12:14 PM

Posting Permissions