DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> jsp+js實現可排序表格
jsp+js實現可排序表格
編輯:AJAX詳解     

首先,定義一個js:  tableSort.JS:

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();  
    }  
}

然後是使用:

<%@ 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"> 
<Html> 
    <head> 
        <base href="<%=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" href="styles.CSS"> 
    --> 
        <script type="text/Javascript" src="userjs/tableSort.JS"></script> 
    </head> 
 
    <body> 
        <table id="theTable" align="center" border="1"> 
            <thead> 
                <tr> 
                    <td> 
                        標題1  
                    </td> 
                    <td onclick="sort(theTable,1,'int')"> 
                        標題2  
                    </td> 
                    <td onclick="sort(theTable,2,'int')"> 
                        標題3  
                    </td> 
                    <td onclick="sort(theTable,3,'int')"> 
                        標題4  
                    </td> 
                    <td onclick="sort(theTable,4,'int')"> 
                        標題5  
                    </td> 
                    <td onclick="sort(theTable,5,'int')"> 
                        標題6  
                    </td> 
                    <td onclick="sort(theTable,6,'int')"> 
                        標題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> 
</Html>

之後就可以看到效果了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved