Dear All,
I have been trying to do a sort on a grid using the sort function that Amos created and having an interesting problem.
Before I get to the problem let me give some background to this Dynaform.
We are on Ubuntu 16.04 and Community 2.3
The Dynaform works as follows
1. The user selects a field (field1) from a dropdown box (the dropdown is populated by a database query).
2. Field1 then will populate dropdown box 2 (field 2). Field 2 is also populated by a database query with field1 as a variable. Field1 & field2 are cascading dropdowns.
3. The “primary key” is the value that is needed.
4. There is a button that will run a function that populates the first column in a grid.
5. The grid has 16 columns and some of the fields use a database to obtain a value for population and other fields need to be filled in by the user.
6. Column 16 (which gets is value from the database) needs to be the primary column for the sort.
Now for the problem
I have a button that will run the code below. When I click on the button all the row fill with the values from the last field. These are only on the fields that use database connections. The values filled in by the user remains unsorted.
I thought the problem was that it was using a shallow copy and when I tried a deep copy the problem still continued.
Based on the sort the function that I am using is as follows
Code: Select all//set column no as the sort field
var colNo = 15;
//set to the ID of the grid:
var gridId = 'gridVar';
//Sort Function
function sortFunction (a,b){
var aUpper = parseInt(a[colNo]);
var bUpper = parseInt(b[colNo]);
if (aUpper === bUpper) {
return 0;
}
else {
return (aUpper < bUpper) ? -1 : 1;
}
}
$("#form\\[sortButton\\]").click( function(){
var oGrid = $("#"+gridId);
var aGridVals = oGrid.getValue();
aGridVals.sort(sortFunction);
//delete all existing rows:
var nRow = oGrid.getNumberRows();
for (; nRow > 0; nRow--) {
oGrid.deleteRow(nRow);
}
//add the sorted rows to the grid:
for (var i = 0; i < aGridVals.length; i++) {
var aRow = [];
for (var ii = 0; ii < aGridVals[0].length; ii++) {
aRow.push( {value: aGridVals[i][ii]} );
}
oGrid.addRow(aRow);
}
});
Does anyone have some suggestion on how to sort the grid and entire rows as I have run out of ideas.
Thanks