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 相关文章推荐
实现checkbox全选、反选、取消JavaScript小脚本异常
Apr 10 Javascript
jquery trigger伪造a标签的click事件取代window.open方法
Jun 23 Javascript
JS中三目运算符和if else的区别分析与示例
Nov 21 Javascript
jQuery选择器源码解读(七):elementMatcher函数
Mar 31 Javascript
JavaScript编码风格指南(中文版)
Aug 26 Javascript
JavaScript 数组的深度复制解析
Nov 02 Javascript
Bootstrap导航条鼠标悬停下拉菜单
Jan 04 Javascript
Angularjs渲染的 using 指令的星级评分系统示例
Nov 09 Javascript
vue中的inject学习教程
Apr 24 Javascript
Vue表单绑定的实例代码(单选按钮,选择框(单选时,多选时,用 v-for 渲染的动态选项)
May 13 Javascript
vue计算属性无法监听到数组内部变化的解决方案
Nov 06 Javascript
vue实现两个组件之间数据共享和修改操作
Nov 12 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
php下使用iconv需要注意的问题
2010/11/20 PHP
php导入excel文件到mysql数据库的方法
2015/01/14 PHP
PHP 数组基本操作小结(推荐)
2016/06/13 PHP
PHP 数组遍历foreach语法结构及实例
2016/06/13 PHP
PHP url的pathinfo模式加载不同控制器的简单实现
2016/08/12 PHP
PHP常见的几种攻击方式实例小结
2019/04/29 PHP
laravel 输出最后执行sql 附:whereIn的使用方法
2019/10/10 PHP
Laravel 类和接口注入相关的代码
2019/10/15 PHP
PHP实现简易用户登录系统
2020/07/10 PHP
js 上下左右键控制焦点(示例代码)
2013/12/14 Javascript
jQuery插件kinMaxShow扩展效果用法实例
2015/05/04 Javascript
详解jQuery插件开发方式
2016/11/22 Javascript
JS获取鼠标位置距浏览器窗口距离的方法示例
2017/04/11 Javascript
bootstrap手风琴折叠示例代码分享
2017/05/22 Javascript
jquery自定义显示消息数量
2017/12/19 jQuery
node内置调试方法总结
2018/02/22 Javascript
vue.js打包之后可能会遇到的坑!
2018/06/03 Javascript
JavaScript 点击触发复制功能实例详解
2018/11/02 Javascript
JavaScript碎片—函数闭包(模拟面向对象)
2019/03/13 Javascript
javascript实现计算器功能
2020/03/30 Javascript
详解JavaScript中的链式调用
2020/11/27 Javascript
从0开始的Python学习016异常
2019/04/08 Python
TensorFlow 读取CSV数据的实例
2020/02/05 Python
详解python变量与数据类型
2020/08/25 Python
Python eval函数介绍及用法
2020/11/09 Python
HTML5新标签兼容——&gt; 的两种方法
2018/09/12 HTML / CSS
美国知名珠宝首饰品牌:Gemvara
2017/10/06 全球购物
Fabletics官网:美国运动服饰品牌,由好莱坞女演员凯特·哈德森创立
2019/10/19 全球购物
Vrbo西班牙:预订您的度假公寓(公寓、乡村房屋…)
2020/04/27 全球购物
JavaScript获取当前url根目录(路径)
2014/02/19 面试题
行政经理岗位职责
2013/11/09 职场文书
社会保险接收函
2014/01/12 职场文书
八项规定自查自纠报告及整改措施
2014/10/26 职场文书
大学生党员暑假实践(活动总结)
2019/08/21 职场文书
springboot利用redis、Redisson处理并发问题的操作
2021/06/18 Java/Android
SQL Server使用导出向导功能
2022/04/08 SQL Server