|
|
| Author |
Message |
ConeSnail
 Total Messages 4
|
| Subject: | Applying same script to many fields. |
Hello,
I've got a pdf that is calculating a large amount of fields. I'm wanting to use the same javascript function for each total column.
Beginning Mileage | Ending Mileage | Reimbursable
---------------------------------------------------
Odometer.1 | Odometer.2 | Total.1
---------------------------------------------------
Odometer.3 | Odometer.4 | Total.2
I suppose I could just create a function something like this:
function reimbursableMileage(ending, beginning)
{
var tempEnd = this.getField("Odometer." + ending).value;
var tempBegin = this.getField("Odometer." + beginning).value;
var Total = tempEnd - tempBegin;
event.value = Total;
}
I'm just wanting to make sure this is the faster way of applying this script to hundreds of Reimbursable Mile Fields.
Thanks for any help/advice!
Posted: 22 Jan 2010 11:13 AM Originally Posted: 22 Jan 2010 11:11 AM |
|
|
| |
jkitzy
 Total Messages 94
|
| Subject: | Applying same script to many fields. |
This will be easier if you change the fields to something like this:
Beginning Mileage | Ending Mileage | Reimbursable
---------------------------------------------------
Start.0 | End.0 | Total.0
---------------------------------------------------
Start.1 | End.1 | Total.1
This is important because every item in the collection has the same child identifiers. Then, you can put a calculation event in one of the Total fields:
///////////////////
var a = this.getField("Total");
var b = a.getArray();
var c; var d; var e;
for(var z = 0;z
c = this.getField("Start." + z).value*1;
d = this.getField("End." + z).value*1;
if(d!=0){this.getField("Total." + z).value = d-c}}
///////////////////
This will cycle through every set and perform the calculation.
If you need a grand total field, then make these additions and put the script in the GrandTotal field:
///////////////////
var a = this.getField("Total");
var b = a.getArray();
var c; var d; var e; ,var f = 0;
for(var z = 0;z
c = this.getField("Start." + z).value*1;
d = this.getField("End." + z).value*1;
if(d!=0){this.getField("Total." + z).value = d-c}
f=f+(d-c);}
event.value = f
///////////////////
Cheers.
---jak
| Posted: 23 Jan 2010 02:52 AM |
|
|
| |
gkaiseril
 Total Messages 3000
|
| Subject: | Applying same script to many fields. |
Using some more advance JavaScripting, on can reference an array of fields and then create an array of field objects and use one or all of the arrays to process the form. One does need to carefully name their fields. Having field names 'start.#', 'end.#', and 'total.#' and 'grandTotal', one could use the following script as the custom calculation script for the 'grandTotal' field:
function Difference (fEnd, fStart) {
return fEnd - fStart;
}
// obtain the total field highest level
var oTotal = this.getField('total');
// convert oTotal oject to an array of member fields
var aTotal = oTotal.getArray();
// make array for start and end
var aStart = this.getField('start').getArray();
var aEnd = this.getField('end').getArray();
var fGrandTotal = 0; // variable for Grand Total
var fTotal = 0; // variable for difference
// use elements of aToal array to control computation of each row
for( i = 0; i < aTotal.length; i++){
fTotal = 0;
// compute total for row i if end is not zero
if(aEnd[i].value !=0) {
fTotal = Difference(aEnd[i].value, aStart[i].value);
}
// update total field
aTotal[i].value = fTotal;
// add result to grand total
fGrandTotal += fTotal;
}
// update field value for grand total
event.value = fGrandTotal;
| Posted: 23 Jan 2010 06:20 AM |
|
|
| |
ConeSnail
 Total Messages 4
|
| Subject: | Applying same script to many fields. |
Thanks for the good advice on this everyone. I'm implementing something similar to what gkaiseril posted in his example.
My problem now is some sort of indexing problem that occurs on the next to last field on the first page. Anything after that gets thrown off.
beg.8 end.8 total.8 <----- calculation works
beg.9 end.9 total.9 <----- total shows @ total.11
beg.10 end.10 total.10 <----- total shows @ total.12
--------------------------------------------------
beg.11 end.11 total.11
I'm guessing this is a problem with the page update but so far I haven't been able to find anything about it in the SDK.
| Posted: 07 Feb 2010 02:47 PM |
|
|
| |
|
|