0

Iam trying to bring some records using php and do some calculations. What iam doing now is that, each rows is having a dropdown with different currencies. When i select each currency, it calculates and shows certain values. Till here its working fine.

What i am trying to achieve is that if i select first currency dropdown, it should calculate the complete records calculations instead of selecting the currency of each rows. I guess i need to do some kind of loop in the jquery which calculates the rows.

Fiddle

Following is the part of jquery script for the currency dropdown.

$(window).load(function() {
  $(document).ready(function() {
    $("select").on('change', function() {

      var dept_number = $(this).val();
      var price = $(this).find(':selected').data('price');
      var selected = $(this).find('option:selected').text();

      if (selected == "INR") {
        $(this).closest('table').find('.total1').val($(this).closest('table').find('.total').val());
      } else {
        $(this).closest('table').find('.total1').val((($(this).closest('table').find('.total').val() * price) / $(this).closest('table').find('.inrvalue').val()).toFixed(3));
      }

      $(this).closest('table').find('.price_unit').val(($(this).closest('table').find('.total1').val() / $(this).closest('table').find('.qty').val()).toFixed(3));

    });
  });
});

i guess i need to add some loops here in this jquery. Anyone to guide me how to do this. Or i need to follow a different step.

This is what i have tried as per the suggestion from Leonix.

$(document).ready(function(){
            $(this).closest('table').find("select").each(function() {
               var dept_number = $(this).val();
               var price = $(this).find(':selected').data('price');
           
                var selected = $(this).find("select");
           
           if(selected=="INR")
            {
               $(this).closest('table').find('.total1').val($(this).closest('table').find('.total').val());
           
            } else
            {
           
           $(this).closest('table').find('.total1').val((($(this).closest('table').find('.total').val() * price) / $(this).closest('table').find('.inrvalue').val()).toFixed(3));
           }
           
               $(this).closest('table').find('.price_unit').val(($(this).closest('table').find('.total1').val()/$(this).closest('table').find('.qty').val()).toFixed(3));
           
           });
           });
Community
  • 1
  • 1
Sanju Menon
  • 747
  • 1
  • 10
  • 24
  • I dont really get what is your problem... But first at all, $(document).ready() in $(window).load() is useless, you should $(document).ready() (Waiting scripts) OR $(window).load() (Waiting scripts & content, like images). See http://stackoverflow.com/questions/8396407/jquery-what-are-differences-between-document-ready-and-window-load For your problem, try $(this).closest('table').find("tr").each(function() { /* Each TR here */ }) – Loenix Feb 18 '16 at 09:50
  • iam still learning jquery and javascript. i will correct the same. The actual problem iam facing is if you have checked my fiddle. each rows has a currency dropdown. If i change the currency, it calculates and change the value in that particular row. But i want like if i change the first currency dropdown, it should apply or do calculations for the whole of the rows. Am i clear? – Sanju Menon Feb 18 '16 at 09:53
  • If you change the currency of ONE row, it changes it for ALL rows ? – Loenix Feb 18 '16 at 09:58
  • Yes exactly, that is what iam looking for. – Sanju Menon Feb 18 '16 at 09:59

1 Answers1

1

In your select change function, do a each for all rows of your table and find the dropdown:

$(this).closest('table').find("select").each(function() {
    /* Each SELECT here, use $(this) */
})

or, depending of your needs :

$(this).closest('table').find("select").each(function() {
    /* Each TR here, use selectInput */
    var selectInput = $(this).find("select");
})

With the select in hands, use selectInput.val(changedSelectInput.val()) changedSelectInput is the jquery object containing the select who changed.
Using nested anonymous functions, take care, they are executed in the object context, so this and $(this) change depending on the object affected by function.

Advice: Use specific css classes for JS, as select.select-currency instead of select only, put these classes in your code. it will prevent so many mistakes and will save your time.

Note: currency_change[] is not a valid ID, if you dont need to set one, dont.

EDIT

Some code: https://jsfiddle.net/btpxq5ow/6/
What I did ?

  1. Fix tags issues
  2. Fix input in tbody issues, NEVER put it in a tr, tbody, table directly.
  3. Fix some indentation issues
  4. Apply the currency change to all rows
  5. Prevent change event to call itself in an infinite loop
  6. Apply calculation to all rows when they are updated
  7. Fix some code syntax & performance issues

Please check your calculation are right since i modified it, see calculateRowTotals().
There are still a few html/js errorsthat must be fixed.

You will rarely get code from stackoverflow

Loenix
  • 1,057
  • 1
  • 10
  • 23
  • If you have seen the fiddle i have posted. Each rows its taking the row value and calculating something. Each rows the value differs as per the db. But the calculations remain same. I just tried your suggestion. (i have updated the question with what i have tried) but its not returning any values.. Should i try ajax, than trying jquery? – Sanju Menon Feb 18 '16 at 10:23
  • The calculation does not depend on the change, repeat all calculations regardless of the change. It is therefore "abstract" the calculation in another function. I am editing your fiddle. It seems you miss something in your calculation process, you do it for whole table instead the row. – Loenix Feb 18 '16 at 10:27
  • yes, i will wait for your updation of fiddle. Then i will get some idea on how to proceed. – Sanju Menon Feb 18 '16 at 10:33
  • I edited my previous comment, but take care your calculation in independant but you have to take care about the modified field to update other ones, isn't it ? – Loenix Feb 18 '16 at 10:33
  • i could see theres no change in your previous comment... pls acknowledge – Sanju Menon Feb 18 '16 at 10:44
  • i have uploaded the working quot file in a server. The link is http://spreadon.net/quot.html . here you can see when the currency is changed the value is changed... – Sanju Menon Feb 18 '16 at 10:49
  • Anyone to guide me on this issue? – Sanju Menon Feb 18 '16 at 11:08
  • I am working on your fiddle but there is multiple errors in your code. e.g There is multiple table tags for one table. – Loenix Feb 18 '16 at 11:54
  • i understand... i know my code is not perfect. Can you pls manage it? – Sanju Menon Feb 18 '16 at 11:58
  • any luck with the same? – Sanju Menon Feb 18 '16 at 12:31
  • oh my god. you have given me the complete code. No ones help me like this. i really really appreciate this. I will check the calculations and will let u know...Iam so happy and thank you very much. – Sanju Menon Feb 19 '16 at 13:27
  • i was trying to emded the jquery script with my actual code. Its just showing NaN. I know iam making some mistake. Can u pls tell me, what changes you have done to html, so that i can do the same changes to my actual script. – Sanju Menon Feb 20 '16 at 06:19
  • Now its working. i rectified the html issues. But the calculations are wrong. I could see the calculations in the first row is correct, where as second row its wrong. Third row, its correct and forth row its wrong. So alternate rows calculations are wrong. Am i making any mistake? – Sanju Menon Feb 20 '16 at 06:44
  • 1
    i did some small changes, its working absolutely fine now.. Really appreciate the help. Thankyou so much. :) – Sanju Menon Feb 20 '16 at 08:25
  • Need a help on a similiar problem which iam having. Nothing initially i was trying to change INR currency to some other currency values. Now i need a function to change other currencies to INR value. Only difference is the formulae. Can i post as a seperate question? – Sanju Menon Mar 01 '16 at 08:05
  • This is the new question which i have posted: http://stackoverflow.com/questions/35718120/jquery-calculations-as-per-selected-dropdown-value-loop – Sanju Menon Mar 01 '16 at 08:51
  • its the continuation of the script helped by you. – Sanju Menon Mar 01 '16 at 09:05