Table sorting in javascript
I have a table on which I want开发者_高级运维 to apply sorting, I downloaded the sortTable.js, included it in my asp page by <script src="sorttable.js"></script>
, gave the table class as sortable
and have all the headings inside <thead><th>
tag, but still sorting does not seem to work. Am I missing something?
Here's the function I use:
function sortTable( table, colNum )
{
var n = table.rows.length - 1;
do
{
var newn = 0;
for( i = 2 ; i <= n ; i++ )
{
if( table.rows[i-1].cells[colNum].innerHTML.toUpperCase() > table.rows[i].cells[colNum].innerHTML.toUpperCase() )
{
table.insertBefore( table.rows[i], table.rows[i-1] );
newn = i;
}
}
n = newn;
}
while( n > 0 );
}
table
is the DOM table object and colNum
is the column index in the table (0 based). It assumes there is a header row on the table (which wont be sorted). It will take a couple seconds to sort a large table (hundreds of rows), but smaller tables sort instantly.
精彩评论