javascript 带有滚动条的表格,标题固定,带排序功能.


Posted in Javascript onNovember 13, 2009
//使用要求: 
//1.将表格的Class命名为:sorttableHold, 
//2.表格放置在一个div中,此div设有overflow属性. 
//3.表格要求有ID,div要求有ID 
//4.要有JQuery.min.js文件 
//5.OK. 
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("sorttableHold") != -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" '+ 
'onclick="ts_resortTable(this, '+i+');return false;">' + 
txt+'<span class="sortarrow">   </span></a>'; 
} 
if(table!=undefined)//分离 
{ 
$('<div id="'+table.id+'Title"></div>').insertBefore('#'+table.parentNode.parentNode.id);//创建DIV 
var div_title=document.getElementById(table.id+'Title'); 
var title = table.cloneNode(true)//all再复制给title 
for(i = title.rows.length -1;i >0;i--)//把title中内容全部删除,只留第一行,也就是标题 
title.deleteRow(i) 
table.deleteRow(0)//GridView中第一行(标题)删除,也就只有内容了 
div_title.appendChild(title) //标题给div 
$("#"+div_title.id+" table:eq(0)").attr("id",div_title.id+"1"); 
$(table).removeAttr("class"); 
} 
} 
function ts_getInnerText(el) { 
if (typeof el == "string") return el; 
if (typeof el == "undefined") { return el }; 
if (el.innerText) return el.innerText; //Not needed but it is faster 
var str = ""; 
var cs = el.childNodes; 
var l = cs.length; 
for (var i = 0; i < l; i++) { 
switch (cs[i].nodeType) { 
case 1: //ELEMENT_NODE 
str += ts_getInnerText(cs[i]); 
break; 
case 3: //TEXT_NODE 
str += cs[i].nodeValue; 
break; 
} 
} 
return str; 
} 
function ts_resortTable(lnk,clid) { 
// get the span 
var span; 
for (var ci=0;ci<lnk.childNodes.length;ci++) {//获取标题中的Span 
if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci]; 
} 
var spantext = ts_getInnerText(span);//标题Span内容 
var td = lnk.parentNode;//单元格 
var column = clid || td.cellIndex;//列 
var tablecurrent = getParent(td,'TABLE');//表 
var tableid=tablecurrent.id.substring(0,tablecurrent.id.length-6); 
var table=document.getElementById(tableid); 
// Work out a type for the column 
if (table.rows.length <= 1) return; 
var itm = ts_getInnerText(table.rows[0].cells[column]); 
sortfn = ts_sort_caseinsensitive; 
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date; 
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date; 
if (itm.match(/^[?]/)) sortfn = ts_sort_currency; 
if (itm.match(/^[\d\.]+$/)) sortfn = ts_sort_numeric; 
SORT_COLUMN_INDEX = column; 
var firstRow = new Array(); 
var newRows = new Array(); 
for (i=0;i<table.rows[0].length;i++) { firstRow[i] = table.rows[0][i]; } 
for (j=0;j<table.rows.length;j++) { newRows[j] = table.rows[j]; } 
newRows.sort(sortfn); 
if (span.getAttribute("sortdir") == 'down') { 
ARROW = '  ↑'; 
newRows.reverse(); 
span.setAttribute('sortdir','up'); 
} else { 
ARROW = '  ↓'; 
span.setAttribute('sortdir','down'); 
} 
// We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones 
// don't do sortbottom rows 
for (i=0;i<newRows.length;i++) 
{ if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) table.tBodies[0].appendChild(newRows[i]);} 
// do sortbottom rows only 
for (i=0;i<newRows.length;i++) 
{ if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) table.tBodies[0].appendChild(newRows[i]);} 
// Delete any other arrows there may be showing 
var allspans = document.getElementsByTagName("span"); 
for (var ci=0;ci<allspans.length;ci++) { 
if (allspans[ci].className == 'sortarrow') { 
if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us? 
allspans[ci].innerHTML = '   '; 
} 
} 
} 
span.innerHTML = ARROW; 
$('#'+tableid+' tr:even').css("background-color","FFF7E8"); 
$('#'+tableid+' tr:odd').css("background-color","#CCE8CF"); 
} 
function getParent(el, pTagName) { 
if (el == null) return null; 
else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase 
return el; 
else 
return getParent(el.parentNode, pTagName); 
} 
function ts_sort_date(a,b) { 
// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX 
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]); 
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]); 
if (aa.length == 10) { 
dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2); 
} else { 
yr = aa.substr(6,2); 
if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; } 
dt1 = yr+aa.substr(3,2)+aa.substr(0,2); 
} 
if (bb.length == 10) { 
dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2); 
} else { 
yr = bb.substr(6,2); 
if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; } 
dt2 = yr+bb.substr(3,2)+bb.substr(0,2); 
} 
if (dt1==dt2) return 0; 
if (dt1<dt2) return -1; 
return 1; 
} 
function ts_sort_currency(a,b) { 
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''); 
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''); 
return parseFloat(aa) - parseFloat(bb); 
} 
function ts_sort_numeric(a,b) { 
aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX])); 
if (isNaN(aa)) aa = 0; 
bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX])); 
if (isNaN(bb)) bb = 0; 
return aa-bb; 
} 
function ts_sort_caseinsensitive(a,b) { 
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase(); 
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase(); 
if (aa==bb) return 0; 
if (aa<bb) return -1; 
return 1; 
} 
function ts_sort_default(a,b) { 
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]); 
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]); 
if (aa==bb) return 0; 
if (aa<bb) return -1; 
return 1; 
} 

function addEvent(elm, evType, fn, useCapture) 
// addEvent and removeEvent 
// cross-browser event handling for IE5+, NS6 and Mozilla 
// By Scott Andrew 
{ 
if (elm.addEventListener){ 
elm.addEventListener(evType, fn, useCapture); 
return true; 
} else if (elm.attachEvent){ 
var r = elm.attachEvent("on"+evType, fn); 
return r; 
} else { 
alert("Handler could not be removed"); 
} 
}
Javascript 相关文章推荐
一个获取第n个元素节点的js函数
Sep 02 Javascript
JavaScript插件化开发教程 (四)
Jan 27 Javascript
AngularJS 中文API参考手册
Jul 28 Javascript
微信小程序 教程之模块化
Oct 17 Javascript
关于ES6的六个小特性(二)
Feb 20 Javascript
详解webpack+angular2开发环境搭建
Jun 28 Javascript
使用jQuery实现页面定时弹出广告效果
Aug 24 jQuery
详解vuex中mapState,mapGetters,mapMutations,mapActions的作用
Apr 13 Javascript
浅谈Webpack 是如何加载模块的
May 24 Javascript
微信小程序实现自定义加载图标功能
Jul 19 Javascript
Vue实现点击当前行变色
Dec 14 Vue.js
详解微信小程序「渲染层网络层错误」的解决方法
Jan 06 Javascript
传智播客学习之JavaScript基础篇
Nov 13 #Javascript
用document.documentElement取代document.body的原因分析
Nov 12 #Javascript
JQuery SELECT单选模拟jQuery.select.js
Nov 12 #Javascript
Jquery操作Select 简单方便 一个js插件搞定
Nov 12 #Javascript
jquery控制listbox中项的移动并排序
Nov 12 #Javascript
Jquery 获取表单text,areatext,radio,checkbox,select值的代码
Nov 12 #Javascript
两个select之间option的互相添加操作(jquery实现)
Nov 12 #Javascript
You might like
thinkphp,onethink和thinkox中验证码不显示的解决方法分析
2016/06/06 PHP
php中html_entity_decode实现HTML实体转义
2018/06/13 PHP
laravel 查询数据库获取结果实现判断是否为空
2019/10/24 PHP
javawscript 三级菜单的实现原理
2009/07/01 Javascript
利用javascript的面向对象的特性实现限制试用期
2011/08/04 Javascript
谈谈JavaScript中的函数与闭包
2013/04/14 Javascript
js处理php输出时间戳对不上号的解决方法
2014/06/20 Javascript
常用原生JS兼容性写法汇总
2016/04/27 Javascript
Bootstrap3.0学习教程之JS折叠插件
2016/05/27 Javascript
JS制作类似选项卡切换的年历
2016/12/03 Javascript
ajax异步请求详解
2017/01/06 Javascript
js实现PC端根据IP定位当前城市地理位置
2017/02/22 Javascript
Webpack 4.x搭建react开发环境的方法步骤
2018/08/15 Javascript
[02:16]深扒TI7聊天轮盘语音出处2
2017/05/11 DOTA
[52:00]2018DOTA2亚洲邀请赛 4.1 小组赛 A组加赛 LGD vs Optic
2018/04/02 DOTA
[01:14:12]2018DOTA2亚洲邀请赛4.7 总决赛 LGD vs Mineski 第二场
2018/04/09 DOTA
[57:41]Secret vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
基于Python Numpy的数组array和矩阵matrix详解
2018/04/04 Python
Python实现将Excel转换成为image的方法
2018/10/23 Python
python实现贪吃蛇游戏
2020/03/21 Python
利用pyuic5将ui文件转换为py文件的方法
2019/06/19 Python
Python 计算任意两向量之间的夹角方法
2019/07/05 Python
python画双y轴图像的示例代码
2019/07/07 Python
django formset实现数据表的批量操作的示例代码
2019/12/06 Python
python将logging模块封装成单独模块并实现动态切换Level方式
2020/05/12 Python
Python3内置函数chr和ord实现进制转换
2020/06/05 Python
python Cartopy的基础使用详解
2020/11/01 Python
纯CSS3实现自定义Tooltip边框涂鸦风格的教程
2014/11/05 HTML / CSS
CAT鞋英国官网:坚固耐用的靴子和鞋
2016/10/21 全球购物
图库照片、免版税图片、矢量艺术、视频片段:Depositphotos
2019/08/02 全球购物
事业单位请假制度
2014/01/13 职场文书
学习三严三实对照检查材料思想汇报
2014/09/22 职场文书
局机关干部群众路线个人对照检查材料思想汇报
2014/10/05 职场文书
2015年教师节新闻稿
2015/07/17 职场文书
MySQL命令行操作时的编码问题详解
2021/04/14 MySQL
Python万能模板案例之matplotlib绘制直方图的基本配置
2022/04/13 Python