Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Sort Table Data with Text Fields (Form)

Avatar

Level 2

I have googled and searched the forms for days for an answer, but have been unsucessful.

Since the only thread I found is a year old, I wanted to start a new one instead of bumping the old one

Ref : http://forums.adobe.com/message/3000436

I am looking for a script to sort the data in a form based on user input.

The form is just a table with multiple rows and 3-5 colums (depending on the dept it is used by)

Using the above referenced thread, I was able to get static table to sort, but i need to have all fields be text fields and the users inputs the data.

The example from the other thread of the static:

http://forums.adobe.com/servlet/JiveServlet/downloadImage/2-3000436-35301/364-128/Untitled.png

I will use the same layout, just need it to be a form instead of static.

If anyone can help, or point me in the right direction, that would be great.

Update #1:

I did locate this page:http://cookbooks.adobe.com/post_Sorting_tables_in_dynamic_PDF-18993.html

I got the sort to work and it appears to be mostly what I am looking for, but how do i deal with blank field??

Example, the last three rows are blank ( no data) the sort fails.

Here is the code from the page, how can i modify it to correct for the blank fields?

------------------------------------------------    

var col = 0; // column index

var num = false; // Is numeric column

var OrderAsc = true; // Order of sorting

function SortTable(tableRef,colIndex,isNumeric,Asc)

{

try

{

col = colIndex;

num = isNumeric;

OrderAsc = Asc;

// String variable containing the form data as XML String

var data =  tableRef.saveXML('pretty');

// An XML variable contains the deserialized XML data

var xmlData = xfa.datasets.createNode("dataGroup",tableRef.name);

xmlData.loadXML(data);

// Number of table rows

var rowsCount = xmlData.nodes.length;

//Number of columns in the table

var cols = xmlData.nodes.item(1).nodes.length;

// A two dimensional array contains complete table data

var master = new Array();

// Fill the array with XML data

for(var i=1;i<rowsCount;i++)

{

master[i-1] = new Array();

for(var j=0;j<cols;j++)

{

master[i-1][j] = xmlData.nodes.item(i).nodes.item(j).value;

}

}

//Sort the 2D array

master.sort(sortFunc);

// Re-fill the XML variable with Array data

for(var i=0;i<master.length;i++)

{

for(var j=0;j<master[i].length;j++)

{

xmlData.nodes.item(i+1).nodes.item(j).value = master[i][j];

}

}

// Modify the table data and remerge the form

var result = xmlData.saveXML('pretty');

tableRef.loadXML(result,1,1);

xfa.form.remerge();

}catch(e)

{

app.alert(e)

}

}

// Customize the sort function to handle 2D array and Numeric columns

function sortFunc(a,b)

{

var x = a[col];

var y = b[col];

try

{

if(num)

{

x = parseInt(a[col]);

y = parseInt(b[col]);

}

}catch(e){}

if(OrderAsc)

{

return x==y?0: (x < y ? -1 :1)

}

else

{

return x==y?0: (x < y ? 1 :-1)

}

}

1 Accepted Solution

Avatar

Correct answer by
Level 2
6 Replies

Avatar

Level 2

Hello,

Right now, i can give you an approach rather than providng you a techincal solution to the problem.

There are different ways of running java scripts on the forms.

1.Writing a utility in the form and calling those functions where ever and when ever in need.

2.javascript running behing each form /feild element.

3.a Javascript file, located in windows - programfile - adobe- reader8.0 - javascripts - "" .JS "" file

Regarding your problem,

We can only sort ponly after all the 3 columns are filled for each row.

If no row exits then no function to be called.

if row count>1 then

do bubble sort on the both the rows, putting them in an array. ( Dynamic array)

the array need to be sorted with the "column" selected.

then ,

refill the form elements.

Avatar

Level 2

I am still looking for a solution to this issue.

I have exhaused all my resources to get this working.

Can anyone help me with a script?

Avatar

Level 2

I am suprized no one has a solution to this issue or it has never come up before now.

Avatar

Correct answer by
Level 2

Solution Found!

Avatar

Level 5

Would it be possible for you to post the solution you found?

Thanks
Mark

Avatar

Level 2

The fix was to modify the code from:

master[i-1][j] = xmlData.nodes.item(i).nodes.item(j).value; 

the new lines is

master[i-1][j] = (xmlData.nodes.item(i).nodes.item(j).value == null) ? "" : xmlData.nodes.item(i).nodes.item(j).value;

This allows for null (empty) fields, allowing the sort to work correctly

------ Full Code Below -------

var col = 0;                              // column index

var num = false;                    // Is numeric column

var OrderAsc = true;          // Order of sorting

function SortTable(tableRef,colIndex,isNumeric,Asc)

{

          try

          {

 

                    col = colIndex;

                    num = isNumeric;

                    OrderAsc = Asc;

 

                    // String variable containing the form data as XML String

                    var data =  tableRef.saveXML('pretty');

 

                    // An XML variable contains the deserialized XML data

                    var xmlData = xfa.datasets.createNode("dataGroup",tableRef.name);

                    xmlData.loadXML(data);

 

                    // Number of table rows

                    var rowsCount = xmlData.nodes.length;

 

                    //Number of columns in the table

                    var cols = xmlData.nodes.item(1).nodes.length;

 

                    // A two dimensional array contains complete table data

                    var master = new Array();

 

                    // Fill the array with XML data

                    for(var i=1;i<rowsCount;i++)

                    {

                              master[i-1] = new Array();

                              for(var j=0;j<cols;j++)

                              {

                                        master[i-1][j] = (xmlData.nodes.item(i).nodes.item(j).value == null) ? "" : xmlData.nodes.item(i).nodes.item(j).value;

                              }

                    }

 

                    //Sort the 2D array

                    master.sort(sortFunc);

 

                    // Re-fill the XML variable with Array data

                    for(var i=0;i<master.length;i++)

                    {

                              for(var j=0;j<master[i].length;j++)

                              {

                                        xmlData.nodes.item(i+1).nodes.item(j).value = master[i][j];

                              }

                    }

 

                    // Modify the table data and remerge the form

                    var result = xmlData.saveXML('pretty');

                    tableRef.loadXML(result,1,1);

                    xfa.form.remerge();

 

          }catch(e)

          {

                    app.alert(e)

          }

}

// Customize the sort function to handle 2D array and Numeric columns

function sortFunc(a,b)

{

          var x = a[col];

          var y = b[col];

 

          try

          {

          if(num)

          {

                    x = parseInt(a[col]);

                    y = parseInt(b[col]);

          }

          }catch(e){}

          if(OrderAsc)

          {

                    return x==y?0: (x < y ? -1 :1)

          }

          else

          {

                    return x==y?0: (x < y ? 1 :-1)

          }

}