您的位置:首页 > Web前端 > JavaScript

javascript table排序 这个更简单了,不用改变现在的表格结构

2010-04-23 00:00 766 查看
JavaScript实现可自定义排序的表格 -

BODY {
FONT-SIZE: 0.8em; FONT-FAMILY: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif
}
P {
FONT-WEIGHT: bold; MARGIN-BOTTOM: 0px
}
.tableWidget_headerCell {
BORDER-RIGHT: #aca899 1px solid; BORDER-LEFT: #fff 1px solid; CURSOR: pointer; BORDER-BOTTOM: #c5c2b2 3px solid; BACKGROUND-COLOR: #ece9d8
}
.tableWigdet_headerCellOver {
BORDER-RIGHT: #aca899 1px solid; BORDER-LEFT: #fff 1px solid; CURSOR: pointer; BORDER-BOTTOM: #c5c2b2 3px solid; BACKGROUND-COLOR: #ece9d8
}
.tableWigdet_headerCellDown {
BORDER-RIGHT: #aca899 1px solid; BORDER-LEFT: #fff 1px solid; CURSOR: pointer; BORDER-BOTTOM: #c5c2b2 3px solid; BACKGROUND-COLOR: #ece9d8
}
.tableWidget_headerCell {
BORDER-TOP: #ece9d8 2px solid
}
.tableWigdet_headerCellOver {
BORDER-TOP: #ffc83c 2px solid
}
.tableWidget TBODY .tableWidget_dataRollOver {
BACKGROUND-COLOR: #fff
}
.tableWigdet_headerCellDown {
BORDER-RIGHT: #fff 1px solid; BORDER-TOP: #ffc83c 2px solid; BORDER-LEFT: #aca899 1px solid; BACKGROUND-COLOR: #dbd8c5
}
.tableWidget TD {
PADDING-RIGHT: 2px; PADDING-LEFT: 2px; PADDING-BOTTOM: 2px; MARGIN: 0px; PADDING-TOP: 2px; BORDER-BOTTOM: #eae9e1 1px solid
}
.tableWidget TBODY {
BACKGROUND-COLOR: #fff
}
.tableWidget {
FONT-SIZE: 12px; WIDTH: 400px; FONT-FAMILY: arial
}
DIV.widget_tableDiv {
BORDER-RIGHT: #aca899 1px solid; BORDER-TOP: #aca899 1px solid; OVERFLOW-Y: auto; BORDER-LEFT: #aca899 1px solid; WIDTH: 400px; BORDER-BOTTOM: #aca899 1px solid; HEIGHT: 200px
}
HTML > BODY DIV.widget_tableDiv {
OVERFLOW: hidden; WIDTH: 400px
}
.tableWidget THEAD {
POSITION: relative
}
.tableWidget THEAD TR {
BOTTOM: 0px; POSITION: relative; TOP: 0px
}
.tableWidget .scrollingContent {
OVERFLOW-Y: auto; WIDTH: 100%
}


/*
(C) www.dhtmlgoodies.com, October 2005
This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.
Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.
Thank you!
www.dhtmlgoodies.com
Alf Magne Kalleland
*/
var tableWidget_tableCounter = 0;
var tableWidget_arraySort = new Array();
var tableWidget_okToSort = true;
var activeColumn = new Array();
var arrowImagePath = "images/"; // Path to arrow images
function addEndCol(obj)
{
if(document.all)return;
var rows = obj.getElementsByTagName('TR');
for(var no=0;no=0)a = eval(a);
if(b.indexOf('/')>=0)b = eval(b);
return a/1 - b/1;
}
function sortString(a, b) {
if ( a.toUpperCase() < b.toUpperCase() ) return -1;
if ( a.toUpperCase() > b.toUpperCase() ) return 1;
return 0;
}
function cancelTableWidgetEvent()
{
return false;
}
function sortTable()
{
if(!tableWidget_okToSort)return;
tableWidget_okToSort = false;
/* Getting index of current column */
var obj = this;
var indexThis = 0;
while(obj.previousSibling){
obj = obj.previousSibling;
if(obj.tagName=='TD')indexThis++;
}
var images = this.getElementsByTagName('IMG');
if(this.getAttribute('direction') || this.direction){
direction = this.getAttribute('direction');
if(navigator.userAgent.indexOf('Opera')>=0)direction = this.direction;
if(direction=='ascending'){
direction = 'descending';
this.setAttribute('direction','descending');
this.direction = 'descending';
}else{
direction = 'ascending';
this.setAttribute('direction','ascending');
this.direction = 'ascending';
}
}else{
direction = 'ascending';
this.setAttribute('direction','ascending');
this.direction = 'ascending';
}
if(direction=='descending'){
images[0].style.display='inline';
images[0].style.visibility='visible';
images[1].style.display='none';
}else{
images[1].style.display='inline';
images[1].style.visibility='visible';
images[0].style.display='none';
}
var tableObj = this.parentNode.parentNode.parentNode;
var tBody = tableObj.getElementsByTagName('TBODY')[0];
var widgetIndex = tableObj.id.replace(/[^\d]/g,'');
var sortMethod = tableWidget_arraySort[widgetIndex][indexThis]; // N = numeric, S = String
if(activeColumn[widgetIndex] && activeColumn[widgetIndex]!=this){
var images = activeColumn[widgetIndex].getElementsByTagName('IMG');
images[0].style.display='none';
images[1].style.display='inline';
images[1].style.visibility = 'hidden';
if(activeColumn[widgetIndex])activeColumn[widgetIndex].removeAttribute('direction');
}
activeColumn[widgetIndex] = this;
var cellArray = new Array();
var cellObjArray = new Array();
for(var no=1;no=0;no--){
for(var no2=0;no2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: