Expand my Community achievements bar.

SOLVED

Trouble Adding Total in a Table

Avatar

Level 3

I created a dynamic table with rows that have decimal fields for money and a button to add more rows if needed and a delete row button also and then a total row.  I can get the add total to work but when I add a row the sum total does not recalculate.  I have the xfa.form.recalculate(1); in the button scripts but is there something else I need to do.  Greatly appreciate any help on this.  Thanks!

Another question, hope an easy one. In one column it consists of a drop down list for example 1, 2, 3, 4, 5, 6, 7, 8.  Is there a way when the first drop down is selected lets say at 3 the rest of the drop down auto populate 4..5..6..7..??

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

Okay, set the name of the base year row to baseYear. This will separate it from the repeating row. On the Design Pages you probably only have one row, which is set up to repeating. Set this row's name back to what it was (to suit the script in the click event of the Add/Delete buttons). I am going to assume Row1.

Now the script in the Total field will look like:

$ = baseYear.Amt + Sum(Row1[*].Amt)

Does that help?

Niall

View solution in original post

9 Replies

Avatar

Level 10

Hi,

It may be that your script does not take into account the number of rows or that additional rows can be added. I have an example here that towards the end has a dynamic table and looks at JavaScript and FormCalc methods for summing the rows: http://assure.ly/kUP02y.

Also here was a example done the other day: http://assure.ly/o61hpQ, which might help even though it was done for a static table. Basically it used FormCalc on the basis that the repeating row has the same name (Row1) and the field that you want to sum has the same name in all rows (Amt). The FormCalc in the Total field would be similar to this:

$ = Sum(Row1[*].Amt)

In relation to the dropdowns, this would be possible. You would need to set up the script to look at the value of the dropdown in the previous row and then populate the current dropdown based on that. You would do this in the preOpen event of the dropdown. Have a look here: http://assure.ly/jcTahK.

You would wrap the script in an if statement to check that the current dropdown is not in the first instance of the row (as it would not have a previous row).

Does that help?

Niall

Avatar

Level 3

Ok so the script was helpful and I got it to work.  Now I'm having the issue with the add row and delete row button.  They stopped working once I gave the rows the same name.  Either is there a way to do this when the row are named differently.  For example I have a static row called base year which is there always and the option year row is the repeating row or more rows can be added to the table.  When I name them the same like Row1 it shows up as Row1, Row1[1], etc.  Is there a workaround for this to get the add and delete button to work??

Avatar

Correct answer by
Level 10

Hi,

Okay, set the name of the base year row to baseYear. This will separate it from the repeating row. On the Design Pages you probably only have one row, which is set up to repeating. Set this row's name back to what it was (to suit the script in the click event of the Add/Delete buttons). I am going to assume Row1.

Now the script in the Total field will look like:

$ = baseYear.Amt + Sum(Row1[*].Amt)

Does that help?

Niall

Avatar

Level 3

Awesome this is perfect...thanks so much!!!!  Now all I have to do is figure out the auto-populate thing.

Avatar

Level 3

thank you so much for your help this is perfect so far.  The only question I have left now...is it possible to when a row is added that the number will +1.  For example right now the first two body rows are title base year and option year with the option year as the repeated row.  When the drop down is selected...lets say base year is FY11 and the option year auto populates to FY12. Is there a way when another option year is added it automatically says FY13??  Thanks again!

Avatar

Level 10

Hi,

Yes, there is a way to achieve this.

For the base year row I would use a dropdown (called FY). In the Object > Field palette set the Display Items to FY10, FY11, FY12, etc. Then go to the Object > Binding palette and tick Specify Values. For each of the dropdown items give a corresponding numeric value: 10 for FY10, 11 for FY11, 12 for FY12, etc.

This means that while the user sees FY11, the value of the dropdown is a numeric value 11.

Now for the option year I would use a TextField instead of a dropdown. Then in the Textfield's LayoutReady event I would use the following JavaScript:

this.rawValue = "FY" + (baseYear.FY.rawValue + this.parent.index + 1);

That should (hopefully) work,

Niall

Avatar

Level 3

It's sorta working It looks like it goes +10 instead of 1.  So it would say FY01 and then FY11 when the next row is added.

Avatar

Level 10

Hi,

I have updated the script in this example: http://assure.ly/nzgN4I.

Basically:

  • Need to convert the dropdown rawValue to a number.
  • Do the maths and then convert the result to a string.
  • Add a leading zero if necessary.
  • Put a FY string in front of that.

// First get the base year and add the row index

var fy = Number(baseYear.FY.rawValue) + this.parent.index + 1;

// Then convert fy to a string

fy = fy.toString();

// Then if fy is between 0 and 9, add a leading zero

if (fy.length < 2) {

          fy = "0" + fy;

}

// Then add a leading FY string

this.rawValue = "FY" + fy;

Hope that helps,

Niall

Avatar

Level 3

You are Amazing!!  Thank you so much for the help on this.