Skip to main content
September 12, 2019
Question

Sort data in table

  • September 12, 2019
  • 2 replies
  • 515 views

I have a form that has a table. The columns are First, Last, Phone, Email and Parent. People use this form to fill up their information. I want to be able to sort the content of the table according to the last name with just a click of a button.

 

I found a script online and edited it, but I can't seem to make it work. There's no error but it does nothing when I click the sort button. Can someone identify what I did wrong in the script?

 

Mouse Up - Run Javascript 

 

// get top level name field object;
var oFirst = this.getField("First");
// create an array of name fields below top level field object;
var aFirst = oFirst.getArray();
// repeat for Last
var oLast = this.getField("Last");
var aLast = oList.getArray();
// repeat for Phone
var oPhone = this.getField("Phone");
var aPhone = oPhone.getArray();
// repeat for Email
var oEmail = this.getField("Email");
var aEmail = oEmail.getArray();
// repeat for Parent
var oParent = this.getField("Parent");
var aParent = oParent.getArray();


// add First, Last, Phone, Email and Parent to array of values;
var aValues = new Array();
for(var i = 0; i < aName.length; i++)
{
aValues.push([aFirst[i].value.toString(), aLast[i].value, aPhone[i].value, aEmail[i].value, aParent[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(aFirst.name, aLast.name, aPhone.name, aEmail.name, aParent.name);
for(var i = 0; i < aValues.length; i++)
{
aFirst[i].value = aValues[i][0];
aLast[i].value = aValues[i][1];
aPhone[i].value = aValues[i][2];
aEmail[i].value = aValues[i][3];
aParent[i].value = aValues[i][4];
}
// done;

    This topic has been closed for replies.

    2 replies

    Participant
    February 24, 2024

    function Comparator(a, b) {

        if (a[column] > b[column]) return -1;

        if (a[column] < b[column]) return 1;

        return 0;

    }

     

    var column = 5; //first column

    var aRows = new Array();

    var nr = Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode.instanceManager.count;

     

    for (var i = 0; i < nr; i++) {

        aRows.push([ xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].txtDocumentReference").rawValue           , xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].dttDocumentDate").rawValue

                    ,xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].txtAccountingDocumentType").rawValue      , xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].txtTransactionCurrency").rawValue

                    ,xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].decAmountInTransactionCurrency").rawValue , xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].InvoiceReference").rawValue ]);

    }

     

    aRows.sort(Comparator);

     

    for (var i = 0; i < nr; i++) {

        //xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].Cell1").rawValue = aRows[i][0];

       // xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].Cell2").rawValue = aRows[i][1];

        xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].txtDocumentReference").rawValue             = aRows[i][0];      

        xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].dttDocumentDate").rawValue                  = aRows[i][1];

        xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].txtAccountingDocumentType").rawValue        = aRows[i][2];

        xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].txtTransactionCurrency").rawValue           = aRows[i][3];

        xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].decAmountInTransactionCurrency").rawValue   = aRows[i][4];

        xfa.resolveNode("Form.bdyMain.frmTableBlock.tblLineItems.rowLineItemNode[" + i + "].InvoiceReference").rawValue                 = aRows[i][5];

    }

    Inspiring
    September 12, 2019

    Open the JavaScript console and see if there is any error.

    The Acrobat JavaScript Console (Your best friend for developing Acrobat JavaScript)

    I found 2 variables that were not defined.