for (var field in XFAUtil.NodeIterator(NumericField))
{
console.println(field.somExpression + " has a value of " + field.rawValue);
}
{
console.println(field.somExpression + " has a value of " + field.rawValue);
}
An iterator is used by assigning a generator function to the __iterator__ method, so the NodeIterator object used above looks like;
function NodeIterator(node)
{
var nodeList = node.parent.resolveNodes(node.name + "[*]");
var limit = nodeList.length;
var current = 0;
return {
__iterator__ : function ()
{
return {
next : function ()
{
if (current >= limit) throw StopIteration;
return nodeList.item(current++);
}
}
}
}
}
The problem with this is the StopIteration exception gets thrown when we get to the end of the list. This probably won't be a problem for the end user but is a pain for the developers who will typically have the "When exception thrown" option set to break.
We can hide this by adding a forEach method similar to the one available for an array. For example to sum the values of an array we can do;
var total = 0;
[1,2,3,4].forEach(function(v) { total += v; });
console.println(total); // prints 10
[1,2,3,4].forEach(function(v) { total += v; });
console.println(total); // prints 10
Similarly we can now sum up all the values of all the numeric fields called NumericField using;
var total = 0;
XFAUtil.NodeIterator(NumericField).forEach(function(v) { total += v.rawValue; });
total;
XFAUtil.NodeIterator(NumericField).forEach(function(v) { total += v.rawValue; });
total;
Or process all the fields of the same type using the NodeClassIterator
var total = 0;
XFAUtil.NodeClassIterator(CheckBox1).forEach(function(v) { if (v.rawValue === 1) total++; });
MessageText.presence = (total >= 2) ? "hidden" : "visible"
XFAUtil.NodeClassIterator(CheckBox1).forEach(function(v) { if (v.rawValue === 1) total++; });
MessageText.presence = (total >= 2) ? "hidden" : "visible"
It may still be better to use the good old for loop, the main advantage of using this technic is it hide some of the XFA stuff. But maybe if you have some rule regarding all rows with a a quantity value greater than 100 then it would be good to put this logic in the one place.
Here's a sample form using the approach, CustomIterator.pdf.
No comments:
Post a Comment