For Sorting I have used two dropdowns:
For displaying the Fields on which sorting is to be done
For displaying the sort order - Ascending or Descending
The dropdown, which shows the Field, picks up the values
from the Header Table tblHeader.
The Header Text could be anything, but make sure you place
it in correct order since it uses the index of the Column to identify the Column
Name in the DataTable. For example, if the CustomerID is the first Column, that
is index value 0 in the DataTable. So it should be first here so that the index
value is picked up correctly.
Listing 16
function PopulateDropDown()
{
var tblHeader = document.getElementById("tblHeader");
var ddlSort = document.getElementById("ddlSort");
var tblCells = tblHeader.getElementsByTagName("TD");
for (var i=0; i<tblCells.length;i++)
{
var opt = document.createElement("option");
ddlSort.options.add(opt);
opt.text = tblCells[i].innerHTML;
opt.value = i;
}
}