DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> javascript小組件 原生table排序表格腳本(兼容ie firefox opera chrome)
javascript小組件 原生table排序表格腳本(兼容ie firefox opera chrome)
編輯:關於JavaScript     
首先創建html頁面為sort.html ,並把下面的內容復制進去
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style type="text/css">
.desc span{ display:none;}
.asc em{ display:none;}
</style>
<script type="text/javascript" src="sort.js"></script>
</head>
<body>
<table width="200" border="1" cellpadding="0" cellspacing="0" sort="true" id="mytab">
<thead style="cursor:pointer">
<tr>
<td class="desc">ID<span>|</span><em>-</em></td>
<td class="desc">name<span>|</span><em>-</em></td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>開心</td>
</tr>
<tr>
<td>3</td>
<td>開飯</td>
</tr>
<tr>
<td>5</td>
<td>開放</td>
</tr>
</tbody>
</table>
<table width="200" border="1" sort="true" id="mytabs">
<thead style="cursor:pointer">
<tr>
<td class="desc">ID<span>|</span><em>-</em></td>
<td class="desc">name<span>|</span><em>-</em></td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>開心</td>
</tr>
<tr>
<td>3</td>
<td>開飯</td>
</tr>
<tr>
<td>5</td>
<td>開放</td>
</tr>
</tbody>
</table>
</body>
</html>


新建腳本頁 sort.js
復制代碼 代碼如下:
/*
表格排序功能
事件:2012 7 24
DOM 節點
如果表格需要排序 在表格屬性中添加 sort="true"
並且 id是唯一並且是必須的
此js文件直接引入即可
因為制作的倉儲 沒有注釋
<table width="200" border="1" sort="true" id="mytab">
<thead style="cursor:pointer">
<tr>
<td class="desc">ID<span>|</span><em>-</em></td>
<td class="desc">name<span>|</span><em>-</em></td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>開心</td>
</tr>
<tr>
<td>3</td>
<td>開飯</td>
</tr>
<tr>
<td>5</td>
<td>開放</td>
</tr>
</tbody>
</table>
*/
var Core = (function(window){
return {
init: function(){
Core.getTableById();
Core.setHeadClick();
},
I: function(id){
return document.getElementById(id);
},
N: function(name){
return document.getElementsByTagName(name);
},
config: {
arr:[]
},
getTableById: function(){
var table_arr=Core.N("table");
for(var i = 0; i < table_arr.length; i++){
if(table_arr[i].getAttribute("sort")){
Core.config.arr.push(table_arr[i].getAttribute("id"));
}
}
//alert(Core.config.arr)
},
setHeadClick: function(){
var arr = Core.config.arr;
if(!arr.length) return false;
for(var j = 0 ; j< arr.length;j++){
var thead = Core.I(arr[j]).getElementsByTagName("thead")[0].getElementsByTagName("td");
for(var i = 0; i<thead.length;i++){
if(thead[i].attachEvent){
thead[i].attachEvent("onclick",Core.sortList)
}else{
thead[i].addEventListener("click",Core.sortList,false)
}
//if(i==0){
// thead[i].click();
// }
}
}
},
sortList: function(e){
var index=0,arr=[],sort="asc",table=null;
if(e.srcElement){
index=e.srcElement.cellIndex;
sort=e.srcElement.getAttribute("sort");
e.srcElement.className=sort == "asc" ? "desc" : "asc";
e.srcElement.setAttribute("sort", sort == "asc" ? "desc" : "asc");
table = Core.getTableId(e.srcElement)
}else{
index=e.currentTarget.cellIndex;
sort=e.currentTarget.getAttribute("sort");
e.currentTarget.className=sort == "asc" ? "desc" : "asc";
e.currentTarget.setAttribute("sort", sort == "asc" ? "desc" : "asc");
table = Core.getTableId(e.currentTarget)
}
Core.getList(table,index,arr);
Core.updateList(table,sort,arr);
},
getTableId: function(p){
for(var i=0,n=p;n=n.parentNode;i++){
if(i>100) break;
if(n.nodeName=="TABLE"){
//alert(n.nodeName/*n.getAttribute("id")*/)
return n;
}
}
},
getList: function (table,index,arr){
var table = table.getElementsByTagName("tbody")[0];
for(var i = 0; i< table.rows.length; i++){
var item = table.rows[i];
for(var j = 0; j< item.cells.length;j++){
var jtem = item.cells[index];
if(jtem.innerHTML){
//alert(jtem.innerHTML)
arr[i]=jtem.innerHTML;
}
break;
}
}
},
updateList: function (table,sort,arr){
var table = table.getElementsByTagName("tbody")[0];
for(var i = 0; i< arr.length;i++){
for(var j = i+1;j< arr.length;j++){
if(sort=="asc"){
if(arr[i] > arr[j]){
var rwos=table.rows[i].cloneNode(true);
table.replaceChild(table.rows[j],table.rows[i]);
if(j+1==arr.length){
//table.insertBefore(rwos,null);
table.appendChild(rwos)
}else{
table.insertBefore(rwos,table.rows[j]);
}
var tim= arr[i];
arr.splice(i,1,arr[j]);
arr.splice(j,1,tim);
}
}else{
if(arr[i] < arr[j]){
var rwos=table.rows[i].cloneNode(true);
table.replaceChild(table.rows[j],table.rows[i]);
if(j+1==arr.length){
//table.insertBefore(rwos,null);
table.appendChild(rwos)
}else{
table.insertBefore(rwos,table.rows[j]);
}
var tim= arr[i];
arr.splice(i,1,arr[j]);
arr.splice(j,1,tim);
}
}
}
}
}
};
})(window);
window.onload=Core.init;
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved