These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Help needed with count of non-null field of repeating subform

Michael Frommer
Registered: Apr 3 2009
Posts: 80

Please tell me where I've gone wrong. My script isn't working correctly.

I need a count of a filled-in (non-null) text field of a repeating subform (subform1). The count will be calculated in a numeric field of a different subform (subform 2).
* I need the count to be automatically updated when another instance of subform1 is added and the text field of subform 1 is filled in.
* If an instance is added, but the text field is not filled-in, it should not be included in the count.
* Also, if an instance is removed, the count of the remaining instances should automatically recalculate.

I am using LCD v.8.05.

I have placed the execCalculate() command in the EXIT event of subform 1's text field (which is part of the repeating subform).

I have placed the following script in the CALCULATE event of subform 2's numeric field.

var aName = xfa.resolveNodes("C1.Registration.Attendee[*]");  // get all instances of text field "Attendee" of repeating subform "Registration".
for (i=0; i<aName.length; i++)    // loop through all instances. 
{
if ((aName.item(i).rawValue != null) && (aName.item(i).rawValue.length > 0))    // If text field is not null 
    {
    var nCount = 0;
    nCount++;    // increment count
    }
}
nCount;    // RawValue of numeric field is total count.

The result is "1" when I fill-in the first instance of the text field, but the count doesn't change when other instnces are filled-in.

My Product Information:
LiveCycle Designer, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
the "nCount" variable needs to be declared and initialized above and outside the loop. Right now it's being reinitailized to 0 each itteration.

Just in general, the calculate event is called each time any form field is modified. It is not called for structural changes on the form. So the execCalculate() fucntion should only be called for structural changes, i.e., place it in the scripts you use for adding and removing instances, not in the exit event of a text field.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Michael Frommer
Registered: Apr 3 2009
Posts: 80
Thanks Thom.

I did delete the execCalculate() call from the text field.
If I understood you correctly, since I already have [b]xfa.form recalculate(1); [/b] in my add/delete buttons I dont need to call the execCalculate() separately. Please tell me if I've come to the wrong conclusion.

I made the change you told me, [b]but the count is still not updating[/b]. The result remains as "1" as long as at least one of the repeating fields is filled-in. Now I have:
var aName = xfa.resolveNodes("C1.Registration.Attendee[*]");var nCount = 0;for (i=0; i<aName.length; i++){if ((aName.item(i).rawValue != null) && (aName.item(i).rawValue.length > 0)){nCount++;}}nCount;
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
The LiveCycle Scripting Reference has a nice example of counting fields.
I just added one more if expression to check only fields name "Attendee".
With the following script TextField1 shows the number if all fields of this name in the form.

Form1.Page1.TextField1::calculate - (JavaScript, client)for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++){var oFields = xfa.layout.pageContent(nPageCount, "field");var nNodesLength = oFields.length;var nCount = 0; for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++){if (oFields.item(nNodeCount).ui.oneOfChild.className == "textEdit"){if (oFields.item(nNodeCount).name == "Attendee"){nCount++;}}}this.rawValue = nCount;}

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

Michael Frommer
Registered: Apr 3 2009
Posts: 80
radzmar,

I think you misunderstood. I am [u]not[/u] counting the number of instances that the field 'Attendee' appears. I am counting only those instances of 'Attendee' that are filled-in (with a name). So, if the subform is repeated 4 times, but only the first three are filled in, the count result should be "3".
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Just add one more if expression to the script to exclude all empty fields.

for (var nPageCount = 0; nPageCount < xfa.host.numPages; nPageCount++){var oFields = xfa.layout.pageContent(nPageCount, "field");var nNodesLength = oFields.length;var nCount = 0; for (var nNodeCount = 0; nNodeCount < nNodesLength; nNodeCount++){if (oFields.item(nNodeCount).ui.oneOfChild.className == "textEdit"){if (oFields.item(nNodeCount).name == "ContractorName"){if (oFields.item(nNodeCount).rawValue != null){nCount++;}}}}this.rawValue = nCount;}

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

Michael Frommer
Registered: Apr 3 2009
Posts: 80
I finally found the error. The '[*]' indicating all instances was in the wrong place. I had it after the text field name, instead of being after the subform name.

So I guess the loop wasn't looping after all, but now it is with the variable changed to:

var aName = xfa.resolveNodes("C1.Registration[*].Attendee");
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Congrats on finding the bug!! That's a sneaky one.

I was going to suggest that you add some "console.println()" statements to test activity in your script. I find this is the best way to nail down exactly the kind of issue you were dealing with.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script