本文实例讲述了jquery实现表格本地排序的方法。分享给大家供大家参考。具体实现方法如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jquery 表格排序</title>
<style type="text/css">
thead
{
background-color: Blue;
color: White;
}
tr.odd
{
background-color: #ddd;
}
tr.even
{
background-color: #eee;
}
.clickable
{
text-decoration: underline;
}
.hover
{
background-color: #5dd354;
}
.sorted
{
background-color: #ded070;
}
.page-number
{
color: Black;
margin:2px 10px;
padding:2px 5px;
}
.active
{
border:solid 1px red;
background-color:#76a7d2;
}
.pager
{
margin-bottom:10px;
margin-left:20px;
}
</style>
<script type="text/javascript" language="javascript" src="js/jquery1.3.2.js"></script>
<script type="text/javascript" language="javascript">
$(function() {
jQuery.fn.alternateRowColors = function() { //做成插件的形式
$('tbody tr:odd', this).removeClass('even').addClass('odd'); //隔行变色 奇数行
$('tbody tr:even', this).removeClass('odd').addClass('even'); //隔行变色 偶数行
return this;
};
$('table.myTable').each(function() {
var $table = $(this); //将table存储为一个jquery对象
$table.alternateRowColors($table); //在排序时隔行变色
$('th', $table).each(function(column) {
var findSortKey;
if ($(this).is('.sort-alpha')) { //按字母排序
findSortKey = function($cell) {
return $cell.find('sort-key').text().toUpperCase() + '' + $cell.text().toUpperCase();
};
} else if ($(this).is('.sort-numeric')) { //按数字排序
findSortKey = function($cell) {
var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''));
return isNaN(key) ? 0 : key;
};
} else if ($(this).is('.sort-date')) { //按日期排序
findSortKey = function($cell) {
return Date.parse('1 ' + $cell.text());
};
}
if (findSortKey) {
&n

