Jsp+js Implementing sortable Tables

Source: Internet
Author: User

function sort (tableId, Sortcolumn,nodetype) {
var table = document.getElementById ("thetable");
var tablebody = table.tbodies[0];
var tablerows = tablebody.rows;

var rowarray = new Array ();
for (var i = 0; i < tablerows.length; i++) {
Rowarray[i] = tablerows[i];
}
if (Table.sortcolumn = = SortColumn) {
Rowarray.reverse ();
} else {
Rowarray.sort (Generatecomparetr (SortColumn, NodeType));
}
var tbodyfragment = document.createdocumentfragment ();
for (var i = 0; i < rowarray.length; i++) {
Tbodyfragment.appendchild (Rowarray[i]);
}
Tablebody.appendchild (tbodyfragment);
Table.sortcolumn = SortColumn;
}

function Generatecomparetr (SortColumn, NodeType) {
return function Comparetr (Trleft, trright) {
var leftvalue = convert (Trleft.cells[sortcolumn].firstchild.nodevalue, nodeType);
var rightvalue = convert (Trright.cells[sortcolumn].firstchild.nodevalue, nodeType);
if (Leftvalue < Rightvalue) {
return-1;
} else {
if (Leftvalue > Rightvalue) {
return 1;
} else {
return 0;
}
}
};
}

function convert (value, DataType) {
Switch (dataType) {
Case "int":
return parseint (value);
Case "float":
return parsefloat (value);
Case "Date":
return new Date (Date.parse (value));
Default
return value.tostring ();
}
}

Then it is used:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base <%=basePath%> ">

<title>my JSP ' tabel.jsp ' starting page</title>

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >
<!--
<link rel= "stylesheet" type= "Text/css" >
-
<script type= "Text/javascript" src= "Userjs/tablesort.js" ></script>

<body>
<table id= "thetable" align= "center" border= "1" >
<thead>
<tr>
<td>
Title 1
</td>
&LT;TD onclick= "Sort (thetable,1, ' int ')" >
Title 2
</td>
&LT;TD onclick= "Sort (thetable,2, ' int ')" >
Title 3
</td>
&LT;TD onclick= "Sort (thetable,3, ' int ')" >
Title 4
</td>
&LT;TD onclick= "Sort (thetable,4, ' int ')" >
Title 5
</td>
&LT;TD onclick= "Sort (thetable,5, ' int ')" >
Title 6
</td>
&LT;TD onclick= "Sort (thetable,6, ' int ')" >
Title 7
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
</tr>
<tr>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
</tr>
<tr>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
<td>
2
</td>
</tr>
<tr>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
</tr>
<tr>
<td>
5
</td>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
</tr>
<tr>
<td>
6
</td>
<td>
7
</td>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<td>
4
</td>
<td>
5
</td>
</tr>
</tbody>
</table>
</body>

Jsp+js Implementing sortable Tables

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.