Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

How to calculate the number of row in a dynamic table

Avatar

Former Community Member
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.
13 Replies

Avatar

Level 3
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);

Avatar

Level 2
Hi Robert,



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



Regards,

Zoe

Avatar

Level 4
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

Avatar

Level 2
That does help - thank you so much Tom!

Avatar

Former Community Member
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.

Avatar

Former Community Member
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

Avatar

Former Community Member
Thanks Paul, I will try that.

Only have one question to get me started: Where should this scripiting go?

Avatar

Former Community Member
Whereever you need to know th enumber of rows.

Avatar

Former Community Member
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

Avatar

Former Community Member
If you are using Acrobat you can hit the Ctrl-J key to view the Javascript console. This will tell you if there is an error in your script.

Avatar

Former Community Member
Paul,<br />When I use the Javascript Console it gives me the following message:<br /><br />missing ) after argument list<br />9:XFA:form1[0]:Diario[0]:Button3[0]:click<br /><br />But I can't find the place where I should add the missing )<br /><br />Here is my script again (as it is currently):<br />-----------------------------------------------------------------<br />var rowIM=form1.Diario.Clocking.Row1.instanceManager.count+2;<br />var a=0<br />for(var i=0;i<rowIM.count;i++) <br /><br />{<br /> <br /> if(xfa.resolveNode("form1.Diario.Clocking.Row1["+i"].Function").rawValue="2") <br /> {<br /><br /> a++; <br /><br /> } <br />}<br />Diario.NumericField1.rawValue=a<br /><br />-----------------------------------------------------------------<br /><br />Can you give me any clue?<br /><br />Thanks<br /><br />Fabricio BRAGA

Avatar

Former Community Member
Two things I see right away ....the 9: in the error message indicates the line number where the error occurred. The reformatting of your code in this dialog makes it impossible for me to know which is line 9. You can right mouse click in the script editor and use the Goto Line option to get to th eright spot. Note that it coudl be on the previous line as well.



Second in javascript a comparison between two thing sis done using doube equal signs (==) and an assignment statement is done using single equal sign(=). In your if statement you are assigning the string 2 to the Row.Function ...you are not comparing the two strings ....therefore the code inside your if statement will never be executed.



Paul