面向对象的JavaScript的表格排序问题
2010-01-11 10:37
344 查看
JavaScript表格排序有很多种方式,不过在使用面向对象的方式进行JavaScript排序时IE会有一些问题。代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>演示表格的排序功能</title>
</head>
<script type="text/javascript">
var previousColumnIndex = 0;
ArrayUtil = {
arrayOne: [3, 32, 2, 5],
arrayTwo: ["3","32","2","5"],
demoNoarmal: function(){
this.arrayOne.sort();//默认方式排序
alert(this.arrayOne);
},
comparionAsc: function(one, two){ //升序
if(one < two){
return -1;
}else if(one > two){
return 1;
}else{
return 0;
}
},
comparionDesc: function(one, two){ //降升
if(one < two){
return 1;
}else if(one > two){
return -1;
}else{
return 0;
}
},
comparionSort: function(){
//this.arrayOne.sort(this.comparionAsc);
this.arrayOne.sort(this.comparionDesc);
alert(this.arrayOne);
},
comparionAscForString: function(one, two){
//return one.localeCompare(two); //升序
return -one.localeCompare(two); //降序
},
comparionString: function(){
this.arrayTwo.sort(this.comparionAscForString);
alert(this.arrayTwo);
},
compareTRs: function(trOne,trTwo){
/*var valueOne = trOne.cells[0].firstChild.nodeValue;
var valueTwo = trTwo.cells[0].firstChild.nodeValue;
return valueOne.localeCompare(valueTwo);*/
var vValue1 = trOne.cells[0].firstChild.nodeValue;
var vValue2 = trTwo.cells[0].firstChild.nodeValue;
if (vValue1 < vValue2) {
return -1;
} else if (vValue1 > vValue2) {
return 1;
} else {
return 0;
}
},
sortTable: function(){
var table = document.getElementById("tableSort");
var tBody = table.tBodies[0];
var columData = tBody.rows;
var trs = new Array;
// alert(columData.nodeType + columData.nodeName + columData.nodeValue);
for(var i = 0; i < columData.length; i++){
trs[i] = columData[i]; //对IE无效
//trs.push(columData[i]); //对IE有效,但是有问题
}
//对表格列内容排序
trs.sort(this.compareTRs);
var fragment = document.createDocumentFragment();
for(var i = 0; i < trs.length; i++){
//fragment.appendChild(trs[i]);
fragment.appendChild(trs.pop());//IE可以使用,但是有问题!!
}
//显示排序后的表格
table.appendChild(fragment);
}
};
</script>
<body>
<input type="button" value="默认排序方式" onclick="ArrayUtil.demoNoarmal()" />
<input type="button" value="比较排序方式" onclick="ArrayUtil.comparionSort()" />
<input type="button" value="字符串数组" onclick="ArrayUtil.comparionString()" />
<table border="1" id="tableSort">
<thead onclick="ArrayUtil.sortTable()" style="cursor:pointer">
<tr>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Smith</td>
</tr>
<tr>
<td>Johnson</td>
</tr>
<tr>
<td>Henderson</td>
</tr>
<tr>
<td>Williams</td>
</tr>
<tr>
<td>Gillaim</td>
</tr>
<tr>
<td>Walker</td>
</tr>
</tbody>
</table><br/>
<input type="button" value="表格排序" onclick="ArrayUtil.sortTable()" />
</body>
</html>
把以上代码拷贝到一个HTML文档,如果使用FixFox和Google浏览器都会正常显示,但是,如果使用IE来看,你会发现有非常奇怪的问题。本人现在还没有搞清楚为什么?对于IE,我非常郁闷!!!!
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>演示表格的排序功能</title>
</head>
<script type="text/javascript">
var previousColumnIndex = 0;
ArrayUtil = {
arrayOne: [3, 32, 2, 5],
arrayTwo: ["3","32","2","5"],
demoNoarmal: function(){
this.arrayOne.sort();//默认方式排序
alert(this.arrayOne);
},
comparionAsc: function(one, two){ //升序
if(one < two){
return -1;
}else if(one > two){
return 1;
}else{
return 0;
}
},
comparionDesc: function(one, two){ //降升
if(one < two){
return 1;
}else if(one > two){
return -1;
}else{
return 0;
}
},
comparionSort: function(){
//this.arrayOne.sort(this.comparionAsc);
this.arrayOne.sort(this.comparionDesc);
alert(this.arrayOne);
},
comparionAscForString: function(one, two){
//return one.localeCompare(two); //升序
return -one.localeCompare(two); //降序
},
comparionString: function(){
this.arrayTwo.sort(this.comparionAscForString);
alert(this.arrayTwo);
},
compareTRs: function(trOne,trTwo){
/*var valueOne = trOne.cells[0].firstChild.nodeValue;
var valueTwo = trTwo.cells[0].firstChild.nodeValue;
return valueOne.localeCompare(valueTwo);*/
var vValue1 = trOne.cells[0].firstChild.nodeValue;
var vValue2 = trTwo.cells[0].firstChild.nodeValue;
if (vValue1 < vValue2) {
return -1;
} else if (vValue1 > vValue2) {
return 1;
} else {
return 0;
}
},
sortTable: function(){
var table = document.getElementById("tableSort");
var tBody = table.tBodies[0];
var columData = tBody.rows;
var trs = new Array;
// alert(columData.nodeType + columData.nodeName + columData.nodeValue);
for(var i = 0; i < columData.length; i++){
trs[i] = columData[i]; //对IE无效
//trs.push(columData[i]); //对IE有效,但是有问题
}
//对表格列内容排序
trs.sort(this.compareTRs);
var fragment = document.createDocumentFragment();
for(var i = 0; i < trs.length; i++){
//fragment.appendChild(trs[i]);
fragment.appendChild(trs.pop());//IE可以使用,但是有问题!!
}
//显示排序后的表格
table.appendChild(fragment);
}
};
</script>
<body>
<input type="button" value="默认排序方式" onclick="ArrayUtil.demoNoarmal()" />
<input type="button" value="比较排序方式" onclick="ArrayUtil.comparionSort()" />
<input type="button" value="字符串数组" onclick="ArrayUtil.comparionString()" />
<table border="1" id="tableSort">
<thead onclick="ArrayUtil.sortTable()" style="cursor:pointer">
<tr>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Smith</td>
</tr>
<tr>
<td>Johnson</td>
</tr>
<tr>
<td>Henderson</td>
</tr>
<tr>
<td>Williams</td>
</tr>
<tr>
<td>Gillaim</td>
</tr>
<tr>
<td>Walker</td>
</tr>
</tbody>
</table><br/>
<input type="button" value="表格排序" onclick="ArrayUtil.sortTable()" />
</body>
</html>
把以上代码拷贝到一个HTML文档,如果使用FixFox和Google浏览器都会正常显示,但是,如果使用IE来看,你会发现有非常奇怪的问题。本人现在还没有搞清楚为什么?对于IE,我非常郁闷!!!!
相关文章推荐
- javascript-jquery-tablesorter的问题:如果表头是多行的,点击排序好像就不正确了
- javascript table排序 这个更简单了,不用改变现在的表格结构
- javascript表格随机排序代码
- javascript中json的去重复、统计、排序、数据整理问题案例介绍
- CxGrid表格中文排序错误问题解决方法
- javascript 二维排序表格代码
- JavaScript控制网页内表格TABLE排序
- JavaScript 对表格排序
- javascript页面表格排序
- JavaScript 给表格排序
- javascript页面表格排序
- JavaScript实现的数据表格:冻结列、调整列宽和客户端排序
- javaScript 实现表格排序
- Javascript中给动态生成的表格添加样式,JavaScript里setAttribute的问题
- javascript实现表格排序 编辑 拖拽 缩放
- JavaScript的排序问题
- JavaScript实现表格排序
- javascript操作表格排序实例分析
- JavaScript实现表格排序方法
- HTML、CSS和JavaScript学习五(案例分析三表格排序及颜色变换)