You are viewing limited content. For full access, please sign in.

Question

Question

Script seems to break my table and lookups

asked on August 11, 2014 Show version history

Hi guys,

 

I have a strange situation that I have not encountered before, although I've implemented many similar projects.

 

I have a table in a form that is populated from a lookup. The lookup value is based on an invoice number, which will then return all valid stock codes for that specific invoice number. This will then populate valid stock codes in the table's "Stock Code" drop down. Then, when the user selects one of the stock codes, it will automatically populate lookup data for "Qty Ordered" and "Total Price". Then, I have some script that calculates the unit price, and upon the user entering the "Qty Returned" value, the script with calculate a subtotal for each line item. 

 

The lookup rules:

 

 

The form doing lookups:

 

 

Add qty returned:

 

This all works wonderfully, until I add a new row. Then my table looks like this:

 

 

1. the extra row I add does not correctly perform the lookup as it did with the first row. The Total Price lookup value is missing, although the Qty Ordered lookup value is correct.

2. the table row formatting is broken. There's no currency symbol displayed for the Price columns and there's also no "X" button at the end of the row that allows you to remove the row.

 

The only way I can get the formatting problem fixed is to remove my script and run the form without doing any calculations. Then everything functions fine...but then I have no calculations.

 

Here's my script:

$(document).ready(function () {
  //trigger for updating expense table values
$('.tblstock').on('change', 'input', sumtotalcredit);
$('.tblstock').on('change', 'select', sumtotalcredit);
$('.handlingCharge').on('change', 'input', sumtotalcredit);  
  function sumtotalcredit() 
  {

    var totalcredit = 0;
    var totalVAT = 0;
    var subtotalcredit = 0;
    var handlingCharge = $('.handlingCharge input').val() / 100;
    var handlingFee = 0;
    
    $('.tblstock tbody tr').each(function ()
       {
                                      
         var subtotal = 0;
         var qtyOrdered = $(this).find('.qtyOrdered input').val() * 1;
         var qtyReturned = $(this).find('.qtyReturned input').val() * 1;
                                      
         //Check that qty Returned is not more than qtyOrdered
                                      
         if(qtyReturned > qtyOrdered)
         {
           alert('The Qty to be returned cannot be more than the Qty Ordered. Please check the value you typed in.');
           return;
         }
                                      
         var unitPrice = 0;
         var totalPrice = $(this).find('.totalPrice input').val();  
                                      
         unitPrice += totalPrice / qtyOrdered;
                                      
         $(this).find('.unitPrice input').val(unitPrice);
         subtotal += unitPrice * qtyReturned;
         subtotal = subtotal.toFixed(2);
                                      
         subtotalcredit += subtotal *1;
         subtotalcredit = subtotalcredit.toFixed(2);
         $(this).find('.stock_subtotal input').val(subtotal);                                      
       });//end for each tblstock function

    if(handlingCharge > 0)
    {
      handlingFee = subtotalcredit * handlingCharge;
      handlingFee = handlingFee.toFixed(2);
    }//end if
    $('.handlingFee input').val(handlingFee);
    $('.credit_subtotal input').val(subtotalcredit);
    totalVAT += subtotalcredit * 0.14;
    totalVAT = totalVAT.toFixed(2);
    $('.vat input').val(totalVAT);
    totalcredit += subtotalcredit *1; 
    totalcredit += totalVAT * 1;
    totalcredit += handlingFee *1;
    //totalcredit = totalcredit.toFixed(2);
    $('.totalCredit input').val(totalcredit);
  }//end sumtotalcredit function

  
});//end ready and end function

I'm really stumped and don't know where I'm going wrong. It all seems to be related to my script, but I'm not 100% convinced, since I've used this method multiple times before. Hopefully somebody can spot my mistake.

 

FYI, this is running Forms 9.1.1.1517.

 

Thanks

Sheldon

0 0

Answer

SELECTED ANSWER
replied on August 11, 2014

Have you tested this in a browser with a JavaScript debugger? Try opening this form in Firefox with Firebug installed and see if there are any errors. That might narrow it down. It looks like there might be an error when you add a new row.

0 0
replied on August 11, 2014

Hi Eric,

 

Thank you very much for the tip. I installed Firebug and immediately spotted the error. Very nice tool to use in future for Forms scripting.

 

I removed line 40 in the code that I posted, and everything started behaving normally.

 

Legend!

 

Thanks

Sheldon

0 0
replied on August 11, 2014

I'm glad it was that easy! Happy to help!

0 0

Replies

replied on August 11, 2014

i haven't dived too into the code sample, but usually when the first line of something works but the second line does not, we have an iteration issue, OR we have an issue with the code that when it tries to iterate, the last line before the iteration causes the script to stop running. 

 

That's the first places I would check to see.  I will try taking a look at your code later on and see if I can offer some advise. 

 

Also,

 

Have you looked at this to help force a lookup? I am thinking maybe you are not getting the second lookup to work because it's within a table, so using the information below may or may not be helpful in getting that to work. I would try removing some of your custom code, to the point where you can confirm when using multiple rows that the two lookup rules are still working. 

https://answers.laserfiche.com//questions/48879/Auto-Fill-Button

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.