cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to sort a Dynamic Table in Adobe Forms using Form-Level Scripting (JavaScript/FormCalc)

0 Likes
349

Hello Everybody, 

I have an Adobe Form requirement to sort a dynamic data table row printing on the form based on the Material field. Currently, this data is populating without any sorting as it is fetched from the Form Provider Gateway Service. 

There is no scope to do this sorting in the backend using any enhancements. The only option is to sort the data at Adobe level using scripting.

I have tried scripting at the Event 'Form: Ready' for the data row  (Row1) of my Table and referred to blogs but this script isn't working. Below is the hierarchy of my form layout and the current script I am trying: 

 

sarthak_raghav121_0-1763716482485.png

FormCalc script I am trying: 

var tbl = "$._Row1" // Table row
var i = 0 // Source row
var j = 0 // Compared row
var tblLength = xfa.resolveNode( tbl ).all.length // Number of rows
var source // Source concatenated fields to compare
var destination // Compare to concatenated fields
// *** Loop from the first row
for i = 0 upto tblLength - 1 do
  // *** Compare with the subsequence rows
  for j = i + 1 upto tblLength -1 do
    source       = xfa.resolveNode( tbl )[ i ].Itm_no    // Concatenate fields source to compare
    destination  = xfa.resolveNode( tbl )[ j ].Itm_no    // Concatenate fields to compare with
    if ( source > destination ) then
      xfa.resolveNode( tbl).instanceManager.moveInstance( j, i )
    endif
  endfor // j
endfor // i

 

JavaScript Code I tried: 

(function(){  
    var rows = this.resolveNodes("Row1[*]");  
    var rowCount = rows.length;       
    if (rowCount <= 1) return;       
    // Bubble sort    
    for (var i = 0; i < rowCount - 1; i++) {         
       for (var j = 0; j < rowCount - i - 1; j++) {        
            var row1 = rows.item(j);            
            var row2 = rows.item(j + 1);                          
            var mat1 = row1.Itm_no.rawValue || "";             
            var mat2 = row2.Itm_no.rawValue || "";                          
            // Compare and swap if needed             
            if (mat1 > mat2) {                
             // Swap all field values                
              swapFieldValues(row1, row2, "Itm_no");                 
              swapFieldValues(row1, row2, "Pro_desc");                 
              swapFieldValues(row1, row2, "qty_ucomm");                 
              swapFieldValues(row1, row2, "Guid_pric");                 
              swapFieldValues(row1, row2, "Discount");                 
              swapFieldValues(row1, row2, "Unit_price");   
              swapFieldValues(row1, row2, "Net_amt");       
              swapFieldValues(row1, row2, "Vat_Rate");     
              swapFieldValues(row1, row2, "VAT_amt");              
              }         }     }         

  function swapFieldValues(row1, row2, fieldName) {         
  var field1 = row1.resolveNode(fieldName);         
  var field2 = row2.resolveNode(fieldName);                  
  if (field1 && field2) {             
  var temp = field1.rawValue;             
  field1.rawValue = field2.rawValue;             
  field2.rawValue = temp;         }    

}

}

();

Any inputs/suggestions are appreciated. Thanks!

 

Accepted Solutions (0)

Answers (0)