解答|问题1.表格排序
表格排序是对表格而言的,与表格的生成无关。就是说,无论用何种方式产生的表格排序时都一样。这是排序函数,很简单。
分升序和降序,要求传入参数:obj 表格名或id; n 排序列,第一列为0
<script>
function table_sort_asc(obj,n) {
var i,j;
var tab = eval("document.all."+obj);
if(tab.tagName != "TABLE") return;
for(i=0;i<tab.rows.length-1;i++)
for(j=i+1;j<tab.rows.length;j++)
if(tab.rows[i].cells[n].innerHTML > tab.rows[j].cells[n].innerHTML)
tab.rows[i].swapNode(tab.rows[j]);
}
function table_sort_desc(obj,n) {
var i,j;
var tab = eval("document.all."+obj);
if(tab.tagName != "TABLE") return;
for(i=0;i<tab.rows.length-1;i++)
for(j=i+1;j<tab.rows.length;j++)
if(tab.rows[i].cells[n].innerHTML < tab.rows[j].cells[n].innerHTML)
tab.rows[i].swapNode(tab.rows[j]);
}
</script>
<html>
<head>
<script>
//方法1:
function sorttable(){
var arrEnd = [];
var arrNotBegin = [];
for (i=0; i < document.all.table1.rows.length; i++) {
var oTr = new Object();
for (j=0; j < document.all.table1.rows(i).cells.length; j++) {
var strNode = document.all.table1.rows(i).cells(j).innerText;
switch(j)
{
case 0:
oTr.time = strNode;
break;
case 1:
oTr.state = strNode;
break;
case 2:
oTr.topic = strNode;
break;
default:
break;
}
}
if(oTr.state == "未开始")
{
arrNotBegin[arrNotBegin.length] = oTr;
}else{
arrEnd[arrEnd.length] = oTr;
}
}
arrNotBegin.sort(function(){var a1=arguments[0];var a2=arguments[1];if(a1.time>a2.time){return 1}else if(a1.time<a2.time){return -1}else{return 0}});
arrEnd.sort(function(){var a1=arguments[0];var a2=arguments[1];if(a1.time>a2.time){return 1}else if(a1.time<a2.time){return -1}else{return 0}});
var strTable = "<table id='table1' border='1'>";
for(var j=0; j<arrNotBegin.length; j++){
strTable += "<tr>";
strTable += "<td>" + arrNotBegin[j].time + "</td>";
strTable += "<td>" + arrNotBegin[j].state + "</td>";
strTable += "<td>" + arrNotBegin[j].topic + "</td>";
strTable += "</tr>";
}
for(var j=0; j<arrEnd.length; j++){
strTable += "<tr>";
strTable += "<td>" + arrEnd[j].time + "</td>";
strTable += "<td>" + arrEnd[j].state + "</td>";
strTable += "<td>" + arrEnd[j].topic + "</td>";
strTable += "</tr>";
}
strTable += "</table>"
document.all.table1.outerHTML = strTable;
}
//方法2:
//这里的td表示要排序的列
function sorttable(){
var xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async = false;
xmldoc.loadXML(document.all.table1.innerHTML);
var xsldoc = new ActiveXObject("Microsoft.XMLDOM");
xsldoc.async = false;
var strxsl = '' +
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">' +
'<xsl:template match="/TBODY">' +
'<xsl:for-each select="TR" >' +
'<xsl:sort select="TD[2]" order="descending"/>' +
'<xsl:sort select="TD[1]" />' +
'<xsl:copy>' +
'<xsl:apply-templates select="*|node()" />' +
'</xsl:copy>' +
'</xsl:for-each>' +
'</xsl:template>' +
'<xsl:template match="*|node()">' +
'<xsl:copy>' +
'<xsl:apply-templates select="*|node()" />' +
'</xsl:copy>' +
'</xsl:template>' +
'</xsl:stylesheet>'
xsldoc.loadXML(strxsl);
var a = xmldoc.transformNode(xsldoc);
a = a.substring(39);
document.all.table1.outerHTML = '<table id="table1">' + a + '</table&
ASP常见问题及解答(8)
80酷酷网 80kuku.com