TableSort.js表格排序插件使用方法詳解
更新時(shí)間:2017年02月10日 13:55:59 作者:孫瑞
這篇文章主要為大家詳細(xì)介紹了TableSort.js表格排序插件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了TableSort.js表格排序的具體代碼,供大家參考,具體內(nèi)容如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TableSort</title>
<style type="text/css">
table {
border-collapse: collapse;
width: 300px;
}
table caption {
border-right: 1px solid #abc;
border-left: 1px solid #abc;
border-top: 2px solid #000;
border-bottom: 2px solid #000;
background-color: #afd;
}
#sales tr, #sales td {
border: 1px solid #abc;
text-align: center;
}
</style>
</head>
<body>
<table id="sales" summary="summary here">
<caption>
Main Title
</caption>
<col/>
<col/>
<col/>
<thead>
<tr>
<th class="asc">Col1</th>
<th>Col2</th>
<th>Col3</th>
</tr>
</thead>
<tbody>
<tr>
<td>A1</td>
<td>S2</td>
<td>W3</td>
</tr>
<tr>
<td>B1</td>
<td>C2</td>
<td>V3</td>
</tr>
<tr>
<td>C1</td>
<td>X2</td>
<td>K3</td>
</tr>
</tbody>
<!-- tfoot><tr><td cols=3 >other description</td></tr></tfoot -->
</table>
<button onclick="fn()">Test</button>
<script language="javascript">
function TableSort(id) {
this.tbl = document.getElementById(id);
this.lastSortedTh = null;
if (this.tbl && this.tbl.nodeName == "TABLE") {
var headings = this.tbl.tHead.rows[0].cells;
for (var i = 0; headings[i]; i++) {
if (headings[i].className.match(/asc|dsc/)) {
this.lastSortedTh = headings[i];
}
}
this.makeSortable();
}
}
TableSort.prototype.makeSortable = function() {
var headings = this.tbl.tHead.rows[0].cells;
for (var i = 0; headings[i]; i++) {
headings[i].cIdx = i;
var a = document.createElement("a");
a.href = "#";
a.innerHTML = headings[i].innerHTML;
a.onclick = function(that) {
return function() {
that.sortCol(this);
return false;
}
}(this);
headings[i].innerHTML = "";
headings[i].appendChild(a);
}
}
TableSort.prototype.sortCol = function(el) {
var rows = this.tbl.rows;
var alpha = [], numeric = [];
var aIdx = 0, nIdx = 0;
var th = el.parentNode;
var cellIndex = th.cIdx;
for (var i = 1; rows[i]; i++) {
var cell = rows[i].cells[cellIndex];
var content = cell.textContent ? cell.textContent : cell.innerText;
var num = content.replace(/(\$|\,|\s)/g, "");
if (parseFloat(num) == num) {
numeric[nIdx++] = {
value : Number(num),
row : rows[i]
}
} else {
alpha[aIdx++] = {
value : content,
row : rows[i]
}
}
}
function bubbleSort(arr, dir) {
var start, end;
if (dir === 1) {
start = 0;
end = arr.length;
} else if (dir === -1) {
start = arr.length - 1;
end = -1;
}
var unsorted = true;
while (unsorted) {
unsorted = false;
for (var i = start; i != end; i = i + dir) {
if (arr[i + dir] && arr[i].value > arr[i + dir].value) {
var temp = arr[i];
arr[i] = arr[i + dir];
arr[i + dir] = temp;
unsorted = true;
}
}
}
return arr;
}
var col = [], top, bottom;
if (th.className.match("asc")) {
top = bubbleSort(alpha, -1);
bottom = bubbleSort(numeric, -1);
th.className = th.className.replace(/asc/, "dsc");
} else {
top = bubbleSort(numeric, 1);
bottom = bubbleSort(alpha, 1);
if (th.className.match("dsc")) {
th.className = th.className.replace(/dsc/, "asc");
} else {
th.className += "asc";
}
}
if (this.lastSortedTh && th != this.lastSortedTh) {
this.lastSortedTh.className = this.lastSortedTh.className.replace(
/dsc|asc/g, "");
}
this.lastSortedTh = th;
col = top.concat(bottom);
var tBody = this.tbl.tBodies[0];
for (var i = 0; col[i]; i++) {
tBody.appendChild(col[i].row);
}
}
function fn() {
var sales = document.getElementById('sales');
var sortTable = new TableSort('sales');
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- javascript sort()對(duì)數(shù)組中的元素進(jìn)行排序詳解
- Elementui表格組件+sortablejs實(shí)現(xiàn)行拖拽排序的示例代碼
- JS簡(jiǎn)單數(shù)組排序操作示例【sort方法】
- JS拖拽排序插件Sortable.js用法實(shí)例分析
- jQuery實(shí)現(xiàn)使用sort方法對(duì)json數(shù)據(jù)排序的方法
- 基于js 各種排序方法和sort方法的區(qū)別(詳解)
- JavaScript中數(shù)組Array.sort()排序方法詳解
- Sortable.js拖拽排序使用方法解析
- JS sort排序詳細(xì)使用方法示例解析
相關(guān)文章
小程序?qū)崿F(xiàn)輪播每次顯示三條數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)輪播每次顯示三條數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
three.js 制作動(dòng)態(tài)二維碼的示例代碼
這篇文章主要介紹了three.js 制作動(dòng)態(tài)二維碼的示例代碼,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
純js代碼實(shí)現(xiàn)未知寬高的元素在指定元素中垂直水平居中顯示
本章節(jié)介紹一下如何實(shí)現(xiàn)未知寬高的元素在指定元素下實(shí)現(xiàn)垂直水平居中效果,代碼簡(jiǎn)單易懂,需要的朋友可以參考下本文2015-09-09
layui2.0使用table+laypage實(shí)現(xiàn)真分頁(yè)
這篇文章主要為大家詳細(xì)介紹了layui2.0使用table+laypage實(shí)現(xiàn)真分頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07

