6
Using Acrobat JavaScript in Forms
Forms Essentials
In the following example, we define a
compare
function that accepts two parameters,
both of which are user/export value pairs, and compares their user values. For example, if
the first parameter is
["Ohio", "OH"]
and the second parameter is
["Arizona", "AZ"]
,
the
compare
function returns
1,
since
"Ohio"
is greater than
"Arizona"
:
function compare (a,b)
{
if (a[0] < b[0]) return -1; // index 0 means user value
if (a[0] > b[0]) return 1;
return 0;
}
Create a temporary array of values and populate it with the user/export value pairs in your
combo box field. The following code creates the array, iterates through the combo box
items, and copies them into the array:
var arr = new Array();
var f = this.getField("myCombobox");
for (var i = 0; i < f.numItems; i++)
arr[i] = [f.getItemAt(i,false), f.getItemAt(i)];
At this point you may invoke the
Array
object’s
sort
method and replace the items in the
combo box field:
arr.sort(compare); // sort the array using your compare method
f.clearItems();
f.setItems(arr);
To specify whether the combo box automatically formats its entries as numbers,
percentage values, or other specialized formats, you may use the functions shown below in
Their definitions are located in the file
Javascripts\aform.js
:
T
ABLE
6.3
Combobox Formatting Functions
Format
Number
Percentage
Date
Time
Special
Function
AFNumber_Format
AFPercent_Format
AFDate_FormatEx
AFTime_Format
AFSpecialFormat
In all cases, invoke the method and pass in the
"Format"
trigger name as the first
parameter, followed by a script containing a call to one of the functions. For example, the
code below sets up the Number format:
f.setAction("Format", ‘AFNumberFormat(2,0,0,0,"\u20ac",true)’);
N
OTE
:
You may also create Custom formatting.
98
Acrobat JavaScript Scripting Guide