5 Replies Latest reply: Aug 2, 2013 11:02 AM by KarenH RSS

    is it possible that my dynamic actions are cancelling each other out?

    KarenH

      Hi Everyone,

       

      I am trying to simplify my apex 4.2 code with dynamic actions.   I have a tabular form based on a collection.   The tabular forms contains (among many variables) the following:

       

      SELECT
      apex_item.text(1,seq_id,'','','id="f01_'||seq_id,'','') "DeleteRow",
      seq_id,
      seq_id display_seq_id,
      ...
      apex_item.text(10,TO_NUMBER(c010),5,null, null,'f10_'||seq_id,'') Quantity,
      apex_item.text(11,TO_NUMBER(c011),5,null,null, 'f11_'||seq_id,'') Price,
      apex_item.text(12, TO_NUMBER(c012),5,null,null,'f12_'||seq_id,'') Dollars
      from apex_collections where....
      

       

      • when quantity is changed, recalculate total dollars as quantity*price
      • when price is changed, check price boundaries,  recalculate total dollars as quantity*price
      • when dollars changed, recalculate price as quantity/total dollars.
      • when total dollars is changed, recalculate the GRAND TOTAL

       

      I have created a separate Dynamic Action for QUANTITY, PRICE, and DOLLARS....and then another DYNAMIC ACTION which fires when any value in the tabular form changes and writes it to the collection.   When PRICE or DOLLARS changes, everything appears to work properly....if PRICE changes, DOLLARS is recalculated.  If DOLLARS changes, PRICE is recalculated.   I am having a problem with QUANTITY though.  It appears for a split instance that when QUANTITY changes, DOLLARS changes...but only for a moment, and then it goes back to the previous value.  I am wondering if these dynamic actions are canceling each other out?

       

       

      dynamic action on QUANTITY

      event: CHANGE

      selection type: JQUERY SELECTOR

      jquery: input[name='f10']

      true action1- execute javascript code:

      var lSeq = $(this.triggeringElement).closest("tr").find("input[name='f01']").val(); 
      setTotal(lSeq); 
      

       

       

      dynamic action on PRICE

      event: CHANGE

      selection type: JQUERY SELECTOR

      jquery: input[name='f11']

      true action1- execute javascript code:

      var lSeq = $(this.triggeringElement).closest("tr").find("input[name='f01']").val();  
      getPriceBoundaries(lSeq); 
      

       

      dynamic action on DOLLARS

      event: CHANGE

      selection type: JQUERY SELECTOR

      jquery: input[name='f12']

      true action1- execute javascript code:

      var lSeq = $(this.triggeringElement).closest("tr").find("input[name='f01']").val();  
      selectDollarsFocus(lSeq, this.browserEvent);  
      changePrice(lSeq);
      

       

      dynamic action: column change  (for any column in the tabular form)

      event:  CHANGE

      selection type: JQUERY SELECTOR

      jquery: input[name='f10'],input[name='f11'],input[name='f12'], .shark_info, .hms_info

      true action 1: set value javascript expression, p110_id = this.triggeringElement.id

      true action 2: set value javascript expression, p110_value = this.triggeringElement.value

      true action 3: set value pl/sql, p110_seq = rtrim(substr(:P110_ID,5,4),'0')

      true action 3: execute pl/sql

       

      declare  
        v_attr number;  
      
      begin   
        v_attr := TO_NUMBER (SUBSTR (:P110_id, 2, 2));  
        safis_collections.update_column( :P110_SEQ,  
                                         v_attr,  
                                         :P110_VALUE);  
      end; 
      

      true action 4:  refresh region landings

       

       

       

      finally, the javascript functions are:

       

      <script language="JavaScript1.1" type="text/javascript">
      
      function setTotal(row)
      {
         //quantity was entered into form, get values
         var price = $x('f11_'+row);
         var total = $x('f12_'+row);
         var quantity = $x('f10_'+row);
         var nquantity = parseFloat(quantity.value);
         var ntotal;
         var nprice;
         nquantity = nquantity.toFixed(3);
         quantity.value = nquantity;
         //if quantity and price both have values calculate total and save
         if(quantity.value > 0 && price.value > 0)
         {
            ntotal = quantity.value * price.value;
            total.value = ntotal.toFixed(2);
         }
         else
         {
               //if quantity and total both have values calculate price and save
            if(quantity.value > 0 && total.value > 0)
            {
               nprice = total.value/quantity.value;
               price.value = nprice.toFixed(6);
               //check to see if the price entered falls within min/max for that species
               var get = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=PriceBoard',0);
               get.add('SPECIESPRICE',price.value);
               get.add('SEQUENCEID',row);
               gReturn = get.get();
               if ((gReturn == 'Price entered is too high') || (gReturn == 'Price entered is too low')){alert(gReturn);}
            }
            else  if (quantity.value > 0)
                     total.value = '';
                  else
                  { 
                       total.value = '';
                       quantity.value = '';
                  }
         }
        //saveQPD(row);
         setOverallTotal(); 
      }
      function setOverallTotal()
      {
         var total = 0;
         var nTotal;
         for(i=1;i<=rowCount;i++)
         {
            if(parseFloat($x('f12_'+i).value) > 0)
            {
               total = total + parseFloat($x('f12_'+i).value);
            }
         }
         ntotal = total.toFixed(2);
         document.getElementById("P3_TOTAL").value = ntotal;
         var get = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=nullProcess',0);
         get.add('P3_TOTAL',ntotal);
         gReturn = get.get();
      }
      function getPriceBoundaries(row) 
      {
         //price was entered into form get all values
         var quantity = $x('f10_'+row);
         var price = $x('f11_'+row);
         var total = $x('f12_'+row);
         var ntotal;
         var nquantity;
         var nprice = parseFloat(price.value);
         nprice = nprice.toFixed(6);
         price.value = nprice;
         //check to see if the price entered falls within min/max for that species
         var get = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=PriceBoard',0);
         get.add('SPECIESPRICE',price.value);
         get.add('SEQUENCEID',row);
         gReturn = get.get();
         if ((gReturn == 'Price entered is too high') || (gReturn == 'Price entered is too low')){alert(gReturn);}
         //if quantity and price both have a value calculate the total
         if(quantity.value > 0 && price.value > 0)
         {
            ntotal = quantity.value * price.value;
            total.value = ntotal.toFixed(2);
         }
         else
         {
            //if total and price both have a value calculate the quantity
            if(total.value > 0 && price.value > 0)
            {
               nquantity = total.value/price.value;
               quantity.value = nquantity.toFixed(3);
            }
            else
            {
               if(price.value > 0)
                    total.value = '';
               else
               {
                    total.value = '';
                    price.value = '';
               }
            }
         }
         saveQPD(row);
         setOverallTotal();
      }
      function saveQPD(row)
      {
         var quantity = $x('f10_'+row).value;
         var price = $x('f11_'+row).value;
         var total = $x('f12_'+row).value;
         //save quantity
         var get = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=setQuantity',0);
         get.add('SETVALUE',quantity);
         get.add('SEQUENCEID',row);
         gReturn = get.get();
      //   alert(gReturn);
         //save price
         get = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=setPrice',0);
         get.add('SETVALUE',price);
         get.add('SEQUENCEID',row);
         gReturn = get.get();
      //   alert(gReturn);
         //save total
         var get = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=setTotal',0);
         get.add('SETVALUE',total);
         get.add('SEQUENCEID',row);
         gReturn = get.get();
      //   alert(gReturn);
         
      }
      function changePrice(row)
      {
         //total was entered get all rows
         var quantity = $x('f10_'+row);
         var price = $x('f11_'+row);
         var total = $x('f12_'+row);  
         var ntotal = parseFloat(total.value);   
         var nprice;
         var nquantity;
         ntotal = ntotal.toFixed(2);
         total.value = ntotal;
         //if quantity and total were entered calculate price.
         if (quantity.value > 0 && total.value > 0)
         {
            nprice = total.value / quantity.value; 
            price.value = nprice.toFixed(6); 
            var get = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=PriceBoard',0);
            get.add('SPECIESPRICE',price.value);
            get.add('SEQUENCEID',row);
            gReturn = get.get();
               if ((gReturn == 'Price entered is too high') || (gReturn == 'Price entered is too low')){alert(gReturn);} 
         }
         //if price and total were entered calculate quantity.
         if (price.value > 0 && total.value > 0)
         {
            nquantity = total.value / price.value;  
            quantity.value = nquantity.toFixed(3);
         }
         if (price.value > 0 && quantity.value > 0)
         {
             ntotal = quantity.value * price.value;
             total.value = ntotal.toFixed(2);
         }
       
             
         saveQPD(row);
         setOverallTotal();        
      }
      
      function selectDollarsFocus(pRow,event)
      {
          tabPress = 0;
          KeyCheck(event);
          if($x('f11_'+ pRow))
          {
                  if(KeyID == 9)
                  {
                      $x('f14_'+ pRow).focus();
                      onFocusAreaFished(pRow);
                      tabPress = 1;
                  }
          }
          else
          {
              if($x('f18_'+ pRow))
              {
                      if(KeyID == 9)
                      {
                          $x('f18_'+ pRow).focus();
                          tabPress = 1;
                      }
              }
              else
              {
                  if(--pRow <= rowCount)
                      if(KeyID == 9)
                      {
                          $x('f08_'+ pRow).focus();
                          tabPress = 1;
                      }
              }
              
          }
      }
      
      
      
      </script>
      

       

      any thoughts really appreciated!  thanks

        • 1. Re: is it possible that my dynamic actions are cancelling each other out?
          tim fox - Enkitec

          That a lot of javascript.  Couldn't you have used built in functions for get value / set value in your dynamic actions?

          • 2. Re: is it possible that my dynamic actions are cancelling each other out?
            KarenH

            perhaps I could have used funcitons.  I inherited this code, so there are a few methods I am not familiar with.  As I am trying to move to dynamic actions, I am wondering why the change to QUANTITY initially works (so seems the dynamic action fires the javascript function properly), but then the DOLLARS switched back to it's pre-calculated value....so, somehow the DYNAMIC ACTION is being negated.

             

            any thoughts?  thank you.

            • 3. Re: is it possible that my dynamic actions are cancelling each other out?
              tim fox - Enkitec

              Karen,

               

              Since you are using Javascript, you have to determine in what order the scripts are being executed - presumably in the order of the Dynamic Action.  If for some reason a Javascript is executing out of the order you expect, you could see an unpredictable behaviour.  I have an app that uses Dynamic Actions almost everywhere.  I call out to Javascript to refresh a graph and depending on where I put those scripts determined if my graphs got updated properly or not.

               

              I might just do a few experiments with a new Dynamic Action that does a Set Value instead of using the full Javascript code you have.  You can even break your work up into steps in the Dynamic Action to ensure that things are happening in the right order.

               

              TGF

              • 4. Re: is it possible that my dynamic actions are cancelling each other out?
                KarenH

                super!  thanks again for the tip.

                 

                I have created three page items P3_QUANTITY, P3_PRICE and P3_DOLLARS....so, you are suggesting that in the dynamic action (each separate?) that I set the value...so that if P3_QUANTITY changes, then ONLY set the value for P3_QUANTITY.  Do not run the function until perhaps the last Dynamic Action which is also for column change and writes the final calculated value to the collection.

                 

                I will play around and keep you posted.  thanks agan for helping me think through this!

                Karen

                • 5. Re: is it possible that my dynamic actions are cancelling each other out?
                  tim fox - Enkitec

                  Yes, that would be one way to test it.  In my app, and Exadata demo, I run a sql statement and then update a bunch of stuff on the screen when the statement finishes.  I update some page items to show how many records were returned, how long the query ran, etc.  I then run a javascript to update the Flash Chart (cause that's the only way to do it).  This all works beautifully.

                   

                  My problems occurred when I was running java script on page startup to make sure my graphs were reset properly.  Some times it would work properly, other times I would get a javascript error stating that the item I was referring to (my graph) did not exist.  So my browser was executing javascript before my page was fully rendered, sometimes.  I fixed this by ensuring that I ran javascript at the appropriate point in page rendering.