Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to sort the data in the form fields

New Here ,
Oct 19, 2016 Oct 19, 2016

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.

TOPICS
Acrobat SDK and JavaScript , Windows
2.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 19, 2016 Oct 19, 2016

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

...
Translate
LEGEND ,
Oct 19, 2016 Oct 19, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 19, 2016 Oct 19, 2016

Only rows containing the data. How repopulate the fields with sorted names and their prices?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 19, 2016 Oct 19, 2016

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 20, 2016 Oct 20, 2016

Due to my knowledge of javascript I call it magic, but it works! Many thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 17, 2019 Oct 17, 2019
LATEST
Please, can you help me? I have letters with diacritic. How to sort in this case?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines