Copy link to clipboard
Copied
I want to sort the data in the form fields. Fields are arranged in a simple table with 10 rows and the heading "Name" and "Price". I have the fields "name.0", "name.1", "name.3" ... "name.10". Then the fields "price.0", "price.1" ... "price.10".
After filling the fields I want to click on the button "sort" and alphabetically sort the data by the first column.
I use Adobe Acrobat Pro.
Then one has to add a filter function remove names with a length of 0.
A script that will perform this task for any number of fields following you naming convention:
// get top level name field object;
var oName = this.getField("name");
// create an array of name fields below top level field object;
var aName = oName.getArray();
// repeat for price
var oPrice = this.getField("price");
var aPrice = oPrice.getArray();
// add names and price to array of values;
var aValues = new Array();
for(var i = 0; i < aN
Copy link to clipboard
Copied
Do you want to sort all the rows or only the rows with data?
With JavaScript you can only sort an array. One could create an array of the names and then once sorted match the names to the prices in another array and then repopulate the fields with sorted names and their prices.
Copy link to clipboard
Copied
Only rows containing the data. How repopulate the fields with sorted names and their prices?
Copy link to clipboard
Copied
Then one has to add a filter function remove names with a length of 0.
A script that will perform this task for any number of fields following you naming convention:
// get top level name field object;
var oName = this.getField("name");
// create an array of name fields below top level field object;
var aName = oName.getArray();
// repeat for price
var oPrice = this.getField("price");
var aPrice = oPrice.getArray();
// add names and price to array of values;
var aValues = new Array();
for(var i = 0; i < aName.length; i++)
{
aValues.push([aName.value.toString(), aPrice.value]);
}
// filter out items with a name of zero length;
function RemoveEmpty(element)
{
return (element[0].toString().length != 0);
} // end RemoveNull function;
aValues = aValues.filter(RemoveEmpty);
// sort by name, element 0 of item array;
function compare(a, b) {
if (a[0] < b[0]) {
return -1;
}
if (a[0] > b[0]) {
return 1;
}
// a must be equal to b
return 0;
} // end compare function;
// sort array of name and price values;
aValues.sort(compare);
// clear fields of values and reload using sorted values;
this.resetForm(aName.name, aPrice.name);
for(var i = 0; i < aValues.length; i++)
{
aName.value = aValues[0];
aPrice.value = aValues[1];
}
// done;
Copy link to clipboard
Copied
Due to my knowledge of javascript I call it magic, but it works! Many thanks!
Copy link to clipboard
Copied