How to sort the data in the form fields

New Here ,
Oct 19, 2016

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.

Adobe Community Professional
Correct answer by gkaiseril | Adobe Community Professional

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;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

876

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to sort the data in the form fields

New Here ,
Oct 19, 2016

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.

Adobe Community Professional
Correct answer by gkaiseril | Adobe Community Professional

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;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

877

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 19, 2016 0
Adobe Community Professional ,
Oct 19, 2016

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.

Likes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2016 0
Adobe Community Professional ,
Oct 19, 2016

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;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2016 1
New Here ,
Oct 20, 2016

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 20, 2016 0
New Here ,
Oct 17, 2019

Copy link to clipboard

Copied

Please, can you help me? I have letters with diacritic. How to sort in this case?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 17, 2019 0