jquery實(shí)現(xiàn)表格本地排序的方法
本文實(shí)例講述了jquery實(shí)現(xiàn)表格本地排序的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<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'); //隔行變色 奇數(shù)行
$('tbody tr:even', this).removeClass('odd').addClass('even'); //隔行變色 偶數(shù)行
return this;
};
$('table.myTable').each(function() {
var $table = $(this); //將table存儲(chǔ)為一個(gè)jquery對(duì)象
$table.alternateRowColors($table); //在排序時(shí)隔行變色
$('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')) { //按數(shù)字排序
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) {
$(this).addClass('clickable').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); }).click(function() {
//反向排序狀態(tài)聲明
var newDirection = 1;
if ($(this).is('.sorted-asc')) {
newDirection = -1;
}
var rows = $table.find('tbody>tr').get(); //將數(shù)據(jù)行轉(zhuǎn)換為數(shù)組
$.each(rows, function(index, row) {
row.sortKey = findSortKey($(row).children('td').eq(column));
});
rows.sort(function(a, b) {
if (a.sortKey < b.sortKey) return -newDirection;
if (a.sortKey > b.sortKey) return newDirection;
return 0;
});
$.each(rows, function(index, row) {
$table.children('tbody').append(row);
row.sortKey = null;
});
$table.find('th').removeClass('sorted-asc').removeClass('sorted-desc');
var $sortHead = $table.find('th').filter(':nth-child(' + (column + 1) + ')');
//實(shí)現(xiàn)反向排序
if (newDirection == 1) {
$sortHead.addClass('sorted-asc');
} else {
$sortHead.addClass('sorted-desc');
}
//調(diào)用隔行變色的函數(shù)
$table.alternateRowColors($table);
//移除已排序的列的樣式,添加樣式到當(dāng)前列
$table.find('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
$table.trigger('repaginate');
});
}
});
});
});
//分頁
$(function() {
$('table.paginated').each(function() {
var currentPage = 0;
var numPerPage = 10;
var $table = $(this);
$table.bind('repaginate', function() {
$table.find('tbody tr').hide().slice(currentPage * numPerPage, (currentPage + 1) * numPerPage).show();
});
var numRows = $table.find('tbody tr').length;
var numPages = Math.ceil(numRows / numPerPage);
var $pager = $('<div class="pager"></div>');
for (var page = 0; page < numPages; page++) {
$('<span class="page-number"></span>').text(page + 1)
.bind('click', { newPage: page }, function(event) {
currentPage = event.data['newPage'];
$table.trigger('repaginate');
$(this).addClass('active').siblings().removeClass('active');
}).appendTo($pager).addClass('clickable');
}
$pager.insertBefore($table);
$table.trigger('repaginate');
$pager.find('span.page-number:first').addClass('active');
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="myTable paginated">
<thead>
<tr>
<th class="sort-alpha">
Last Name
</th>
<th class="sort-alpha">
First Name
</th>
<th>
</th>
<th class="sort-numeric">
Due
</th>
<th class="sort-date">
Date
</th>
<th>
Web Site
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
tmith
</td>
<td>
erthn
</td>
<td>
eth@gmail.com
</td>
<td>
$34.00
</td>
<td>
14 2009
</td>
<td>
ftp://www.baidu.com
</td>
</tr>
<tr>
<td>
TTmith
</td>
<td>
BJohn
</td>
<td>
jsmith@gmail.com
</td>
<td>
$50.00
</td>
<td>
Mar 2009
</td>
<td>
ftp://www.baidu.com
</td>
</tr>
<tr>
<td>
CSmith
</td>
<td>
John
</td>
<td>
DDDD@gmail.com
</td>
<td>
$50.00
</td>
<td>
Mar 2009
</td>
<td>
http://www.dbjr.com.cn
</td>
</tr>
<tr>
<td>
Smith
</td>
<td>
John
</td>
<td>
sdsf@gmail.com
</td>
<td>
$50.00
</td>
<td>
f32 2009
</td>
<td>
ffttp://www.dbjr.com.cn
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>
希望本文所述對(duì)大家的jquery程序設(shè)計(jì)有所幫助。
- jQuery表格排序組件-tablesorter使用示例
- 基于jquery的表格排序
- Jquery 選中表格一列并對(duì)表格排序?qū)崿F(xiàn)原理
- jquery tablesorter.js 支持中文表格排序改進(jìn)
- jQuery+Ajax實(shí)現(xiàn)表格數(shù)據(jù)不同列標(biāo)題排序(為表格注入活力)
- jquery 表格排序、實(shí)時(shí)搜索表格內(nèi)容(附圖)
- 擴(kuò)展jquery實(shí)現(xiàn)客戶端表格的分頁、排序功能代碼
- jquery.tableSort.js表格排序插件使用方法詳解
- 基于jQuery實(shí)現(xiàn)表格的排序
- jQuery增加和刪除表格項(xiàng)目及實(shí)現(xiàn)表格項(xiàng)目排序的方法
- jQuery html表格排序插件tablesorter使用方法詳解
- jQuery實(shí)現(xiàn)的表格前端排序功能示例
相關(guān)文章
jQuery+PHP+MySQL二級(jí)聯(lián)動(dòng)下拉菜單實(shí)例講解
這篇文章主要介紹了一款jQuery+PHP+MySQL三者結(jié)合實(shí)現(xiàn)的二級(jí)聯(lián)動(dòng)下拉菜單,需要的朋友可以參考下2015-10-10jQuery移動(dòng)web開發(fā)中的頁面初始化與加載事件
這篇文章主要介紹了jQuery移動(dòng)web開發(fā)中的頁面初始化與加載事件,是JavaScript移動(dòng)端頁面開發(fā)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-12-12jQuery實(shí)現(xiàn)的調(diào)整表格行tr上下順序
表格元素是大家比較常用的元素,有時(shí)候表格中的行需要調(diào)整順序,下面通過代碼實(shí)例介紹一下如何利用jquery實(shí)現(xiàn)此功能2016-01-01jquery+ajax實(shí)現(xiàn)直接提交表單實(shí)例分析
這篇文章主要介紹了jquery+ajax直接提交表單的方法,涉及jQuery調(diào)用ajax進(jìn)行表單提交所涉及的表單序列化、數(shù)值傳遞與處理、回調(diào)函數(shù)等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06jQuery使用prepend()方法在元素前添加內(nèi)容用法實(shí)例
這篇文章主要介紹了jQuery使用prepend()方法在元素前添加內(nèi)容的方法,實(shí)例分析了prepend方法追加內(nèi)容的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03網(wǎng)站如何做到完全不需要jQuery也可以滿足簡(jiǎn)單需求
據(jù)統(tǒng)計(jì),目前全世界57.3%的網(wǎng)站使用它。也就是說,10個(gè)網(wǎng)站里面,有6個(gè)使用jQuery。如果只考察使用工具庫的網(wǎng)站,這個(gè)比例就會(huì)上升到驚人的91.7%2013-06-06