js 处理表格排序(中英文)
2007-04-01 21:47
281 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://www.w3.org/TR/html4/loose.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>Sort table</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script>...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var flag1, flag2, flag3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag1 = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag2 = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag3 = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//clear rows for table
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function clearAllRows(oTable)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(var i=oTable.rows.length-1;i>0;i--)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oTable.deleteRow(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function addRows(oTable, Person)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var rowid = oTable.rows.length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var row = oTable.insertRow(rowid);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var cell=row.insertCell();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.innerText = Person.getId();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var cell = row.insertCell();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.innerHTML = Person.getName();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var cell=row.insertCell();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cell.innerHTML=Person.getAge();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function sortBy(index)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var oTable = document.getElementById("t1");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//get data from table
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var arr = new Array();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//skip the first row, because it is the head of the table
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(var i = 1;i < oTable.rows.length; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var id = oTable.rows[i].cells[0].innerText;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var name = oTable.rows[i].cells[1].innerText;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var age = oTable.rows[i].cells[2].innerText;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arr[i - 1] = new Person(id, name, age);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//process sort
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
switch(index)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case 0:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(flag1)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//sort ascending
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
arr.sort(function(a,b)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return parseInt(a.getId()) - parseInt(b.getId());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag1 = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//sort desc
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
arr.sort(function(a,b)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return parseInt(b.getId()) - parseInt(a.getId());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag1 = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case 1:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(flag2)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
arr.sort(function(a,b)...{return a.getName().localeCompare(b.getName());});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag2 = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
arr.sort(function(a,b)...{return a.getName().localeCompare(b.getName());});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
arr.reverse();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag2 = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case 2:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if(flag3)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
arr.sort(function(a,b)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return parseInt(a.getAge()) - parseInt(b.getAge());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag3 = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
}else...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
arr.sort(function(a,b)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return parseInt(b.getAge()) - parseInt(a.getAge());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag3 = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//clear the all rows except the head of the table
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
clearAllRows(oTable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//add the rows
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(var i = 0; i < arr.length; i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
addRows(oTable, arr[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
function Person(id, name, age)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
new Person('', '', '');
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Person.prototype.getId = function() ...{ return this.id;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Person.prototype.getName = function() ...{ return this.name;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Person.prototype.getAge = function() ...{ return this.age;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Person.prototype.toString = function() ...{ return this.name;}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body style="font-size:12px ">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<table width="200" border="1" id="t1">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td><a href="javascript:sortBy(0)">Id</a></td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td><a href="javascript:sortBy(1)">Name</a></td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td><a href="javascript:sortBy(2)">Age</a></td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>1</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>语文</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>108</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>2</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>数学</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>40</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>3</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>化学</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>58</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>4</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>地理</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<td>22</td>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</table>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
说明 数组默认的排序方法不能按照中文拼音字母顺序排序,这里使用新方法locateCompare进行处理
相关文章推荐
- js 表格排序 支持 中英文 , 可选择不排序的行
- JS简单实现表格排序功能示例
- table表格排序js插件(高效简洁)
- JS 实现表格列排序
- js实现表格的排序功能
- js 静态HTML表格排序功能实现
- 改写人家的js表格排序
- 利用MySQL数据库来处理中英文取首字母排序
- Vue.js实现多条件筛选、搜索、排序及分页的表格功能
- markdown-it js 添加表格处理
- 【JS】根据表格ID进行排序(附注释)
- jsp+js实现可排序表格
- js实现表格排序(正序和反序)
- csv表格处理(上)-- JS 与 PHP 协作导入导出
- js拖动表格排序
- JS对表格排序(支持对序号,数字,字母,日期)
- JS实现隔行换色的表格排序
- js实现表格字段本地排序
- TableSort.js表格排序插件使用方法详解
- 对表格按日期或数字或字母进行排序的js