12 Replies Latest reply: Dec 24, 2012 2:16 PM by Earl Lewis RSS

    How to calculate fields in a form?

    976941
      Hello,

      I am new to apex and i have a question about create a simple calculation in a form.

      I created a form and i have 3 fields which i want to use for calculating.

      Field 1/ field 2 = field 3

      when the user enters data into the first and second field it will automatically calculate the third field.

      How can i do this?

      i am using

      Application Express 4.2.1.00.08
        • 1. Re: How to calculate fields in a form?
          Jitendra
          Hi, 973938

          update your username with some meaningful name.

          you are talking about the tabular form or other.give more information.

          you can check previous threads,they already discussed about the same.

          you can check the below threads
          Calculated Column on Tabular Form
          Dynamic Tabular Form sum two columns in another


          hope this will helps you.

          Regards,
          Jitendra
          • 2. Re: How to calculate fields in a form?
            Earl Lewis
            The short answer is that it depends. I'm assuming you want the calculation to appear as soon as the second value is put in, and if this is the case then javascript is the answer. You can create a generic javascript function that does division and takes to input parameters and returns the result. Inside the function you would check to make sure the denominator isn't zero (an illegal operation), do the math and assign the calculated value to the return value of the function.

            The function code would look something like this:
            function div_values(numerator, denominator) {
            // pass page item names into the function
            // $v will get the value from the item name
            if ($v(denominator) != 0) {
             result = $v(numerator)/$v(denominator);
             return result;
            }
            }
            Then you can call the function via a dynamic action by selecting a "Javascript Expression" for a Set Value operation (Advanced dynamic actions) and put this into the Javascript expression area:

            div_values(P1_NUMERATOR,P1_DENOMINATOR)

            The reason I recommend creating this little function as opposed to just doing the math in the javascript expression area is that the function becomes reusable and the page items aren't hard coded into the function. Then you can use the function wherever you want to on the page or, if you put it in a place where every page can get to it, you can use it anywhere in the app.

            Earl
            • 3. Re: How to calculate fields in a form?
              976941
              Thank you both for the help

              i am using the normal form

              do i post that code in the javascript of the page or do i create a function in the object browser?
              • 4. Re: How to calculate fields in a form?
                Earl Lewis
                You would put that function in the header of your page where you want to use it, in the Javascript section, in the area that's labeled "Function and Global Variable Declaration".

                Then you would call that function by putting this into the dynamic action that calculates/sets the page item:
                div_values(P1_NUMERATOR_ITEM,P1_DENOMINATOR_ITEM)
                Using your page item names instead, of course.

                Earl
                • 5. Re: How to calculate fields in a form?
                  976941
                  Hello Earl,

                  I have tried to do it, but doesn't seem to fire or give a value.


                  this is the code i used

                  var htmldb_delete_message='"DELETE_CONFIRM_MSG"';

                  function div_values(l_num_of_share number, l_tot_num_of_shares number) {

                  if ($v(l_tot_num_of_shares) != 0) {
                  result = $v(l_num_of_share)/$v(l_tot_num_of_shares);
                  return result;
                  }
                  }

                  Then when i created the dynamic action

                  Event:on change

                  selection type: Item

                  item name : P5_tot_num_of_shares

                  condition: none


                  Action: set value

                  set type: Java script expression

                  code: calculate_percentage(P5_num_of_share,P5_tot_num_of_shares)

                  affected elements:

                  selection type: P5_Perc


                  Is there anything wrong with what i did that doesn't work?

                  Edited by: eman721 on Dec 23, 2012 4:10 AM
                  • 6. Re: How to calculate fields in a form?
                    695721
                    Salam Eman ,
                    You can use concept in this example
                    http://apex.oracle.com/pls/apex/f?p=2523:1:1283998801198:::::
                    Regards,
                    Ahmed
                    • 7. Re: How to calculate fields in a form?
                      976941
                      Hello Ahmed,

                      Thank you for the link,

                      How did you apply this ? Did you use a tabular form which does it automatically?

                      Edited by: eman721 on Dec 23, 2012 5:19 AM
                      • 8. Re: How to calculate fields in a form?
                        695721
                        Salam Eman ,

                        if you use tabular form , you can use in your HTML Header page something like that
                        <script type="text/javascript">
                        function sumItem(pThis) {
                           var pid = pThis.id.replace("f06" ,"f05");
                           var tid = pThis.id.replace("f06" ,"f07");
                           $s(tid, Number($v(pid)) * Number($v(pThis)));
                        }
                        </script>
                        this operation to calculate ( Price * quantity) = Total

                        if you use text form you can see this example
                        http://apex.oracle.com/pls/apex/f?p=31517:71:15448653023000:::RP,::

                        i make it in my workspace
                        http://apex.oracle.com/pls/apex/f?p=2523:2:2942610947681:::::

                        Best Regards,
                        Ahmed
                        -----
                        If you believe that my answer was helpful or correct, then please mark the answer as helpful or correct.
                        • 9. Re: How to calculate fields in a form?
                          976941
                          Ahmed

                          Yes this is exactly what i need. but here you didn't use tabular form did you?

                          I tried to do it in my form but it still doesn't seem to work :(

                          I don't Have item identified or a value thats stored

                          And here is my problem.

                          Edited by: eman721 on Dec 23, 2012 8:35 AM
                          • 10. Re: How to calculate fields in a form?
                            Earl Lewis
                            Eman721,

                            If the code that you posted is what you're actually using there are several problems. First, you don't declare variable types for parameters in the signature of your javascript function. You only put the parameter names. So you have shown your function as:
                            function div_values(l_num_of_share number, l_tot_num_of_shares number) {
                            
                            if ($v(l_tot_num_of_shares) != 0) {
                            result = $v(l_num_of_share)/$v(l_tot_num_of_shares);
                            return result;
                            }
                            }
                            You need to remove the number declaration for the parameters.

                            Also, when you wrote the call to your function you didn't use the name of your function. You need to call the function by the name that you give it. According to the code you showed you called your function "div_values". Then when you called it in the dynamic action you wrote the call as:
                            calculate_percentage(P5_num_of_share,P5_tot_num_of_shares)
                            If this is the actual state of your page then you will be getting a "function not defined" error if you were to watch your javascript in the console.

                            If you're going to undertake these kinds of development activities you will need to be more careful about your coding practices. Best of luck.

                            Earl
                            • 11. Re: How to calculate fields in a form?
                              976941
                              thank you earl,
                              I am so sorry for that mistake.

                              I copied and pasted quickly. I tried over again, nothing seems to fire. probably there is something wrong with my database.
                              • 12. Re: How to calculate fields in a form?
                                Earl Lewis
                                >
                                I copied and pasted quickly. I tried over again, nothing seems to fire. probably there is something wrong with my database.
                                Actually, what you're doing here has nothing to do with the database, as long as we're talking about javascript and dynamic actions (typically) this is all about the user interface and the client interaction.

                                It's also pretty typical when you're starting out with javascript to make small mistakes that cause things to not work correctly. Don't despair, just be very careful, pay attention to every detail, ask others to look at what you've done and most of all, try to use some sort of web development tools that can help you debug javascript.

                                Haven't used Firefox for a while but when I was using Windows, Firefox and Firebug were my go-to tools. There are web development plug-ins for just about every browser nowadays so find one to fit your browser and learn how to use it. It is time well spent.

                                Earl