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

Question

Question

Grand Total not adding JavaScript Fields

asked on August 15, 2017 Show version history

I have some tables created that adds up the total and they automatically add up to a Grand Total Currency field. I then have a Mileage table that I found out I would need JavaScript to make it work. After putting in the JavaScript and getting everything to work it seems like my Grand Total section doesn't recognize the Grand Total of the Mileage. Does this mean I have to write JavaScript for everything to be able to add? 

 

So my Grand total at the bottom adds up the Grand Total All Expenses to be Reimbursed, but it doesn't seem to read the Grand Total of Mileage Expenses. I did use JavaScript to get the # or Miles * Mileage Rate to fill the Total column as well. (Code is on the bottom). It has been a while since I've done any coding, so I am rusty on some of my knowledge. Thank You.

 

 

0 0

Answer

SELECTED ANSWER
replied on August 15, 2017

Okay, I see what you mean now. You can't use the function to get the product of just one single row. On a side note, don't do x*y, do PRODUCT(x,y). However, that wouldn't give you what you need anyway.

So, try this, add the following function to your javascript.

function setReadOnly(e,t) {
  if(t){
    // Set input element to read-only
    e.attr({'readonly': 'readonly', 'backend-readonly': 'True'});
  } else {
    // Remove readonly from input element
    e.removeAttr('readonly backend-readonly');
  }
}

Then, go into your code and,

  1. call setReadOnly with a false parameter to remove the attribute
  2. update the value and trigger the change event (this is important)
  3. call setReadonly with a true paramater to make it read only again

 

For example,

setReadOnly($('.test input'),false);

$('.test input').val('test').change();

setReadOnly($('.test input'),true);

However, once that is done you should be able to update the "Grand total" mileage to use a function that sums the values of the Total column.

With the change I'm suggesting it should store the values, so I don't see any reason that grand total field would also rely on javascript.

0 0

Replies

replied on August 16, 2017

I'm a bit late to the party here, but I'm lost as to why the forms calculations aren't working for you.

 

Assuming you have all of your field types set correctly (using number fields and currency fields), I was able to replicate your set up and get it working correctly.  

 

For the Mileage Total Field, I used the formula:  =PRODUCT(INDEX(Mileage.Number_of_Miles,ROW()),Index(Mileage.Mileage_Rate,Row()))

(My variable names may be slightly different than yours, but should be close)

1 0
replied on August 16, 2017

I tried yours too and it worked, but it looks like the total has a default value right away even if my number of Miles is blank it's showing that the total is 0.54 because the Mileage rate is pre-filled in. Not sure if you're getting that as well?

0 0
replied on August 16, 2017 Show version history

You're right, I see that as well.  With the currency field only allowing two decimal spots, it rounds.  If you do 1 mile, it stays at 0.54, but 0.99 miles rounds down to 0.53 as it should.

 

You might be able to play with some IF formulas to get it to show up blank.  From my experience however, people haven't had any issues with the default of 0.54 showing.  Making the Number of Miles Required ensures that the value changes.  

1 0
replied on August 16, 2017

Nice I'll play around with some IF formulas to see if I can get it working. The only reason I would want it at 0 is because I have it where the mileage section will only show if they checked the mileage check box, but with it not being at 0 my Grand Total Expense is still shows the 0.54. But thanks for the information I'll play around with it more to see what I can do. 

0 0
replied on August 15, 2017

What was the reason you couldn't use a function calculation on the mileage Total column to fill that field instead of using JavaScript?

Things start getting difficult any time you populate a read-only field using JavaScript, for example, it will not update the variable when the form is submitted and the data will be lost.

In my experience it seems like the functions work off of the back-end variables rather than the contents of the field, so if the data isn't being saved, it is not going to factor into the calculation.

0 0
replied on August 15, 2017

Well I was looking into just auto calculating the # of Miles Multiply by Mileage rate to get the total, but I thought I read somewhere, that it couldn't do that without JavaScript unless I misread it. I have tried to use formula's but I couldn't get it to work. They keep getting errors. Example: 

that doesn't work as well.

 

I also understand what you're saying. That was what I was thinking as well that if a field gets populated with JavaScript it is being "saved" differently compared to having the fields work with formulas. 

0 0
SELECTED ANSWER
replied on August 15, 2017

Okay, I see what you mean now. You can't use the function to get the product of just one single row. On a side note, don't do x*y, do PRODUCT(x,y). However, that wouldn't give you what you need anyway.

So, try this, add the following function to your javascript.

function setReadOnly(e,t) {
  if(t){
    // Set input element to read-only
    e.attr({'readonly': 'readonly', 'backend-readonly': 'True'});
  } else {
    // Remove readonly from input element
    e.removeAttr('readonly backend-readonly');
  }
}

Then, go into your code and,

  1. call setReadOnly with a false parameter to remove the attribute
  2. update the value and trigger the change event (this is important)
  3. call setReadonly with a true paramater to make it read only again

 

For example,

setReadOnly($('.test input'),false);

$('.test input').val('test').change();

setReadOnly($('.test input'),true);

However, once that is done you should be able to update the "Grand total" mileage to use a function that sums the values of the Total column.

With the change I'm suggesting it should store the values, so I don't see any reason that grand total field would also rely on javascript.

0 0
replied on August 15, 2017

It worked! Thanks you so much for your help Jason.

0 0
replied on August 15, 2017 Show version history

I noticed if I copy and paste the first form and add a second form which is identical, the Total field is blank unless I go back into my # of Miles field and click in it, and then tab/click out of it then it repopulates. Is there a way to keep it from disappearing?

 

First Form shows this:

 

But when I get on the Second form it shows this:

 

I feel like it's just another JavaScript code that I have to put into the 2nd form?

0 0
replied on August 15, 2017

Just to clarify, are you saying it is blank when you load the form in a subsequent task? For example, user submits Form 1, then a reviewer approves/rejects using Form 2?

If so, I would submit a test form, then before you open the next form look at the process variables to see if it is actually storing the value to the variable.

You'll want to track down where the data is being lost.

0 0
replied on August 16, 2017 Show version history

Correct, user submits Form 1, and then a reviewer approves/rejects Form 2. I did a test form, and after submitting Form 1 even though the submitter sees that all the information is there, after submission Form 1 itself isn't holding any of the data. Looks like it's saving the other sections that didn't use JavaScript. Would this be due to JavaScript "saving" differently? This is all on the Form 1 after submission.

 

0 0
replied on August 16, 2017

Can you post the revised/updated code from Form 1?

0 0
replied on August 16, 2017 Show version history

Here is the revised JavaScript code from Form 1

0 0
replied on August 16, 2017

Okay, the problem you're having here is that you're disabling the "read only" state of the ".total input" after you already set the value.

The read only changes for that field should go before and after where you set the value for that field, not with the other field. Also, you need to add the .change to the end of that one as well.

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

Sign in to reply to this post.