ASP FAQs and Answers (8)

Source: Internet
Author: User
Tags copy eval sort table name tagname xsl
Solution | Question 1. Table sorting
Table sorting is for tables, regardless of table generation. That is, no matter how the table is generated in the same way as the sort. This is the sort function, very simple.
In ascending and descending order, incoming parameters are required: Obj table name or ID; n row sequence, first column 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>





<script>
Method 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 = = "Not Started")
{
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;
}


Method 2:
The TD here represents the columns to be sorted
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&



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.