[转载]jQuery实现表格按列字母或数字排序

[转载]jQuery实现表格按列字母或数字排序 – 海之澜 – 博客园.

最近做的一个项目中需要自己写个表格排序的功能,于是就上网收集了一下,主要原理就是JQuery操作表格

JQuery代码如下:

$(function () {
$('#tblGrid').each(function () {
var $table = $(this); //将table存储为一个jquery对象

$('thead td', $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;
};
}

if (findSortKey) {
$(this).addClass('clickable').hover(function () { $(this).addClass('hover'); }, function () { $(this).removeClass('hover'); }).click(function () {
//反向排序状态声明
var newDirection = 1;
if ($(this).is('.sorted-asc')) {
newDirection = -1;
}
var rows = $table.find('tbody>tr').get(); //将数据行转换为数组
$.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('thead td').removeClass('sorted-asc').removeClass('sorted-desc');
var $sortHead = $table.find('thead td').filter(':nth-child(' + (column + 1) + ')');
//实现反向排序
if (newDirection == 1) {
$sortHead.addClass('sorted-asc');
} else {
$sortHead.addClass('sorted-desc');
}

//移除已排序的列的样式,添加样式到当前列
$table.find('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
$table.trigger('repaginate');
});
}
});
});
});

Html代码如下:


 
<table id="tblGrid">
<thead>
<tr>
<td style="width: 10%;">员工工号</td>
<td style="width: 10%;">员工姓名</td>
<td style="width: 15%;">职务名称</td>
<td class="sort-numeric" style="width: 10%;">得分</td>
<td class="sort-alpha" style="width: 10%;">绩效考核等级

(直接主管)</td>
<td style="width: 25%;">事例说明</td>
<td class="sort-alpha" style="width: 10%;">绩效考核等级

(上级主管)</td>
<td style="width: 10%;">绩效考核等级

(部门主管)</td>
</tr>
</thead>
<tbody><!--#RowBegin#-->
<tr>
<td height="28px"></td>
<td>张三</td>
<td>客服支持</td>
<td></td>
<td class="director_1">优秀</td>
<td>李四 直接主管考评信息</td>
<td>待改进</td>
<td><select name="Table:model:RANK:0"> <option value="">-请选择-</option></select>
<select name="Table:model:RANK:0"> <option value="优秀">优秀</option></select>
<select name="Table:model:RANK:0"> <option value="优良">优良</option></select>
<select name="Table:model:RANK:0"> <option value="良好">良好</option></select>
<select name="Table:model:RANK:0"> <option selected="selected" value="待改进">待改进</option></select>
<select name="Table:model:RANK:0"> <option value="不胜任">不胜任</option></select></td>
</tr>
<tr>
<td height="28px"></td>
<td>王五</td>
<td>客服支持</td>
<td></td>
<td class="director_2">优良</td>
<td>事例说明</td>
<td>优良</td>
<td><select name="Table:model:RANK:1"> <option value="">-请选择-</option></select>
<select name="Table:model:RANK:1"> <option value="优秀">优秀</option></select>
<select name="Table:model:RANK:1"> <option selected="selected" value="优良">优良</option></select>
<select name="Table:model:RANK:1"> <option value="良好">良好</option></select>
<select name="Table:model:RANK:1"> <option value="待改进">待改进</option></select>
<select name="Table:model:RANK:1"> <option value="不胜任">不胜任</option></select></td>
</tr>
<tr>
<td height="28px"></td>
<td>周氏</td>
<td>客服支持</td>
<td></td>
<td class="director_3">优秀</td>
<td>直接主管考评信息</td>
<td>优秀</td>
<td><select name="Table:model:RANK:2"> <option value="">-请选择-</option></select>
<select name="Table:model:RANK:2"> <option selected="selected" value="优秀">优秀</option></select>
<select name="Table:model:RANK:2"> <option value="优良">优良</option></select>
<select name="Table:model:RANK:2"> <option value="良好">良好</option></select>
<select name="Table:model:RANK:2"> <option value="待改进">待改进</option></select>
<select name="Table:model:RANK:2"> <option value="不胜任">不胜任</option></select></td>
</tr>
<!--#RowEnd#--></tbody>
</table>

css:


 

利用JQuery的ready事件即可实现排序功能,

页面效果如图:

点击查看源代码SortTable.rar

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏