一实用的实现table排序的Javascript类库
2024-09-06 12:41:30
供稿:网友
 
 一个Javascript 的类库,用于table内容排序。使用很方便,不用每次都去调用数据库了。 
特别适合多表查询的排序。加上<tbody>的style.display 切换,也可以实现分页。 
效果演示 
用法: 
1.添加JS 
<SCRIPT src="sorttable.js" type="text/javascript"></SCRIPT> 
2.添加TABLE,注意的是:一定要有ID,class为"sortable" 
<table class="sortable" id="mytable"> 
OK,可以了,简单的吧 
如果觉得太单调,自己加点CSS吧,官方给出了改HEAD的CSS 
/* Sortable tables */ 
table.sortable a.sortheader { 
    background-color:#eee; 
    color:#666666; 
    font-weight: bold; 
    text-decoration: none; 
    display: block; 
} 
table.sortable span.sortarrow { 
    color:black; 
    text-decoration: none; 
} 
 代码如下:
addEvent(window, "load", sortables_init);
var SORT_COLUMN_INDEX;
function sortables_init() {
    // Find all tables with class sortable and make them sortable
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("table");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
            //initTable(thisTbl.id);
            ts_makeSortable(thisTbl);
        }
    }
}
function ts_makeSortable(table) {
    if (table.rows && table.rows.length > 0) {
        var firstRow = table.rows[0];
    }
    if (!firstRow) return;
    // We have a first row: assume it's the header, and make its contents clickable links
    for (var i=0;i<firstRow.cells.length;i++) {
        var cell = firstRow.cells[i];
        var txt = ts_getInnerText(cell);
        cell.innerHTML = '<a href="#" class="sortheader" '+