Sort data in table
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;
