4 Replies Latest reply on Oct 20, 2016 12:13 AM by kamil.cyprian

    How to sort the data in the form fields

    kamil.cyprian Level 1

      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.

        • 1. Re: How to sort the data in the form fields
          gkaiseril MVP & Adobe Community Professional

          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.

          • 2. Re: How to sort the data in the form fields
            kamil.cyprian Level 1

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

            • 3. Re: How to sort the data in the form fields
              gkaiseril MVP & 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[i].value.toString(), aPrice[i].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[i].value = aValues[i][0];
              aPrice[i].value = aValues[i][1];
              }
              // done;

              1 person found this helpful
              • 4. Re: How to sort the data in the form fields
                kamil.cyprian Level 1

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