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 相关文章推荐
JS 控制CSS样式表
Aug 20 Javascript
js实现获取焦点后光标在字符串后
Sep 17 Javascript
jQuery进行组件开发完整实例
Dec 15 Javascript
JavaScript实现九九乘法表的简单实例
Jun 07 Javascript
利用JavaScript实现拖拽改变元素大小
Dec 14 Javascript
Bootstrap modal 多弹窗之叠加关闭阴影遮罩问题的解决方法
Feb 27 Javascript
react开发中如何使用require.ensure加载es6风格的组件
May 09 Javascript
AngularJS对动态增加的DOM实现ng-keyup事件示例
Mar 12 Javascript
微信小程序利用swiper+css实现购物车商品删除功能
Mar 06 Javascript
解决layui的table插件无法多层级获取json数据的问题
Sep 19 Javascript
node静态服务器实现静态读取文件或文件夹
Dec 03 Javascript
基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件功能
Feb 23 Vue.js
传智播客学习之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
PHP中可以自动分割查询字符的Parse_str函数使用示例
2014/07/25 PHP
php获取URL中带#号等特殊符号参数的解决方法
2014/09/02 PHP
php fread读取文件注意事项
2016/09/24 PHP
yii使用bootstrap分页样式的实例
2017/01/17 PHP
原生JS实现Ajax通过GET方式与PHP进行交互操作示例
2018/05/12 PHP
Yii2框架中一些折磨人的坑
2019/12/15 PHP
jQuery ui1.7 dialog只能弹出一次问题
2009/08/27 Javascript
jQuery EasyUI API 中文文档 - Dialog对话框
2011/11/15 Javascript
javascript匿名函数应用示例介绍
2014/03/07 Javascript
jQuery监听文件上传实现进度条效果的方法
2016/10/16 Javascript
vue的一个分页组件的示例代码
2017/12/25 Javascript
vue实现2048小游戏功能思路详解
2018/05/09 Javascript
详解vscode中vue代码颜色插件
2018/10/11 Javascript
详解Webpack loader 之 file-loader
2018/11/07 Javascript
jQuery实现动态添加和删除input框代码实例
2019/03/29 jQuery
如何基于javascript实现贪吃蛇游戏
2020/02/09 Javascript
vue瀑布流组件实现上拉加载更多
2020/03/10 Javascript
文章或博客自动生成章节目录索引(支持三级)的实现代码
2020/05/10 Javascript
解决vue请求接口第一次成功,第二次失败问题
2020/09/08 Javascript
在Python3中初学者应会的一些基本的提升效率的小技巧
2015/03/31 Python
Python 功能和特点(新手必学)
2015/12/30 Python
Python简单实现安全开关文件的两种方式
2016/09/19 Python
Python 40行代码实现人脸识别功能
2017/04/02 Python
Python外星人入侵游戏编程完整版
2020/03/30 Python
Python实现的插入排序算法原理与用法实例分析
2017/11/22 Python
Python 从相对路径下import的方法
2018/12/04 Python
pandas DataFrame 删除重复的行的实现方法
2019/01/29 Python
对Python函数设计规范详解
2019/07/19 Python
讲解Python3中NumPy数组寻找特定元素下标的两种方法
2019/08/04 Python
手把手教你Python yLab的绘制折线图的画法
2019/10/23 Python
护理专业个人求职简历的自我评价
2013/10/13 职场文书
社区清明节活动总结
2014/07/04 职场文书
领导班子党的群众路线教育实践活动对照检查材料
2014/09/25 职场文书
2014年助理政工师工作总结
2014/12/19 职场文书
信仰观后感
2015/06/03 职场文书
前端canvas中物体边框和控制点的实现示例
2022/08/05 Javascript