JScript 利用 Array.sort() 和 Nodes.swap()
來完成重新排列。
但速度比XML的sortNode慢。(-_-メ)
<!--
Copyright(c) 1998-2004 dewin all rights reserved
Start 2003-09-18 15:50
Finish 2003-09-18 16:17
-->
<body>
<script>
/* create the table */
TRlength = 100
letter="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
str = "<table id=test style=''> <tbody> <tr onclick='if(event.srcElement!=this)sortNode(event.srcElement)'><td style='background:#FF69B4'>Letter</td><td style='background:#ADFF2F'>Name</td><td style='background:#87CEFA'>Money</td></tr> </tbody> <tbody>"
for(i=0;i<TRlength;i++)
str += "<tr><td>"+letter.charAt(Math.floor(Math.random()*10000%62))+"</td><td>"+String.fromCharCode(Math.round(Math.random()*(40869-19968)+19968))+"</td><td>"+String(Math.random()).slice(2)+"</td></tr>"
str += "</tbody></table>"
document.write(str)
ary = [];
oTR = test.childNodes[1].childNodes;
oTRorder = test.firstChild.firstChild.childNodes;
for(i=0, j=oTR.length; i<j; i++)//collect TR nodes in Array
ary[i] = oTR[i];
for(i=0, j=oTRorder.length; i<j; i++)
oTRorder[i].order = -1;
function sortNode(SortNode){
var time = new Date()
var tempa, tempb, oTD, orders
oTD = SortNode.sourceIndex-SortNode.parentElement.sourceIndex-1;//which TD we Click
orders = SortNode.order = -SortNode.order;
ary.sort(//sort the nodes here.
function(a,b){
tempa = a.childNodes[oTD].innerText;//you can use a.childNodes[oTD].innerText.length or other things to sort
tempb = b.childNodes[oTD].innerText;
temp = tempa<tempb?-orders:tempa==tempb?0:orders;
return temp;
}
)
for(i=0; i<oTR.length; i++)
oTR[i].swapNode(ary[i]);//swap TR nodes
alert(new Date()-time);
}
</script>