How to calculate the number of row in a dynamic table | Community
Skip to main content
January 15, 2008

How to calculate the number of row in a dynamic table

  • January 15, 2008
  • 13 replies
  • 12875 views
Hi,

I've got a dynamic table where rows can be added by the user, in each row there's a checkbox cell. At the end of the table there's a "fixed" row where I have to count the number of the checked dynamic rows. How can I do that?



Thanks for your help.

Pascal.
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

13 replies

Level 3
January 18, 2008
You can add in a for loop using javascript. This loop will go through each row and check the value of the field and then add that to an incrementing variable. Below is some example code.



//rowIM is the instance Manager for the row

var rowIM = xfa.resolveNode("Form.Page.Table.Row").instanceManager;



var checkedCount = 0;



//Go through each item

for(var i=0;i<rowIM.count;i++)

{

//Check the field value to see if it is checked

if(xfa.resolveNode("Form.Page.Table.Row[" + i + "].CheckField").rawValue = 1)

{

checkedCount++; //Increase the check count

}

}



xfa.host.messageBox('Checked fields: ' + checkedCount);
January 21, 2008
Hi Robert,



It works,

thanks a lot.



Pascal.
Level 2
November 20, 2008
Hi Robert,



how would I count the occurances of a field value = true ON A PAGE?



Regards,

Zoe
Level 5
November 20, 2008
Not so easy to do.



The above script relies on all of the checked fields being the same name, but with a different number in the array (e.g. entry[0], entry[1], entry[2]).



If your fields have different names you need to test each field individually (call each by their own name).



You can use the "==" expression to return a value of 1 if it is correct though, so something like:



{

count = (fieldName == "true") + (fieldSecondName == "false") + (fieldValue == 75);

}



Could return a value of 3 if all of the expressions are true.



Hope this helps,



Tom
Level 2
November 20, 2008
That does help - thank you so much Tom!
November 21, 2008
Good Morning,

I have a similar issue. I have a dynamic table with a fixed header row, one row (instance) that can be added or removed "Row1"and a fixed footer row.

In my repeatable row I have one field with a dropdown list called "Function"(form1.Diario.Clocking.Row1[0].Function) that gives the user 5 choices. Every time the user adds a row he can make a different choice.



The user can choose the following options:

Foreman

Fitter

Welder

Journeyman

Apprentice



Using numeric fields outside the table I want to count how many times each of the functions above appears in the dynamic table so the user knows how many people he has in each function.



I can post my sample if you would tell me how to upload it.

Thanks for any insights.
November 21, 2008
You will need code that does this:



1. Determine how many rows you have. you can use this command:



form1.Dario.Clocking.Row1.instanceManager.count();



2. Build a for loop to loop through each row. Your max value should be the count you just calculated.

3. Inside the for loop test the value of the Function field for the value that you are looking for. Assuming that the count is set to i, to get the value use:



xfa.resolveNode("form1.Dario.Clocking.Row1[" + i "]").Function.rawValue
November 21, 2008
Thanks Paul, I will try that.

Only have one question to get me started: Where should this scripiting go?
November 21, 2008
Whereever you need to know th enumber of rows.
November 21, 2008
So I added a NumericField outside the table with the following code:<br /><br />----- form1.Diario.NumericField1[1]::calculate: - (JavaScript, client) -----------------------------<br />//1. Determine how many rows you have<br />var rowIM=form1.Diario.Clocking.Row1.instanceManager.count;<br />// counter for the searched value<br />var foremanCount=0<br />//2. Build a for loop to loop through each row<br />for(var i=0;i<rowIM.count;i++) <br />{ <br /> //3. Test the value of the Function field for the value that you are looking for <br /> if(xfa.resolveNode("form1.Diario.Clocking.Row1[" + i "]").Function.rawValue ="Foreman") <br /> { <br /> foremanCount++; //Increase the counter<br /> } <br />}<br />this.rawValue=foremanCount<br />---------------------------------------------------------------------<br />What am I doing wrong? My NumericField remains blank no matter what...<br />Thanks