你的位置: Kiyo'Space首页 ASP 阅读文章 欢迎留下您的足迹

ASP常见问题及解答(8)

[ ASP ] 分享

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; {
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>';
}
</script>
</head>
<body>
<table id="table1" border="1">
<tr>
<td>22:30</td>
<td>未开始</td>
<td>倩女幽魂</td>
</tr>
<tr>
<td>21:30</td>
<td>未开始</td>
<td>TV三贱客</td>
</tr>
<tr>
<td>20:30</td>
<td>完</td>
<td>娱乐串串烧</td>
</tr>

<tr>
<td>23:30</td>
<td>未开始</td>
<td>篮球大本营</td>
</tr>
<tr>
<td>22:15</td>
<td>未开始</td>
<td>新闻时间</td>
</tr>
</table>

<input type="button" value="排序" onclick="sorttable();">
</body>
</html>


欢迎在此留下您的脚步






评论内容 (*必填):
(Ctrl + Enter 快速提交)