Category: Javascript Keywords: sort table
写程序的时候用到排序,用 SQL 语句就是 ORDER BY, 不过这要重新 query 一次程序造成负担。所以一般就可以考虑用 javascript 来达成目的。网络里有 n 多用 javascript 做的 sort table.用 Google 搜索了下, http://kryogenix.org/code/browser/sorttable/ 该 javascript library 非常简单。按网页的介绍:
- 将该库下载回来。然后在 HTML 中载入:http://kryogenix.org/code/browser/sorttable/sorttable.js
将 table 的 class 设置为 sortable, 再设置一个独有的 id 就可以了。<script src="sorttable.js"></script> <table class="sortable" id="unique_id"> - 如果嫌它不够好看就改一下 CSS
<style type="text/css"> /* Sortable tables */ table.sortable a.sortheader { background-color:#006DB3; color:white; font-weight: bold; text-decoration: none; display: block; } table.sortable span.sortarrow { color: white; text-decoration: none; } </style>
| Name | Salary | Extension | Start date |
|---|---|---|---|
| Bloggs, Fred | $12000.00 | 1353 | 18/08/2003 |
| Turvey, Kevin | $191200.00 | 2342 | 02/05/1979 |
| Mbogo, Arnold | $32010.12 | 2755 | 09/08/1998 |
| Shakespeare, Bill | $122000.00 | 3211 | 12/11/1961 |
| Shakespeare, Hamnet | $9000 | 9005 | 01/01/2002 |
| Fitz, Marvin | $3300 | 5554 | 22/05/1995 |