英文原文:[url]http://kryogenix.org/code/browser/sorttable/
[/url]
sorttable: Make all your tables sortable
Now, how to use it. To make a table of your choice sortable, there are three steps:
Download the Javascript library
Include the Javascript library, by putting a link to it in the HEAD of your page, like so:
<script src="sorttable.js"></script>
[/script]
Mark your table as a sortable one by giving it a class of "sortable":
<table class="sortable">
Note that the library's JavaScript file is called sorttable (two Ts), but the class you add to the table is sortable (one T).
And that's all you need. Your table will now have column sorting available by clicking the headers. For niceness, you might want to add the following styles to your stylesheet, or make up some of your own based on this:
/* Sortable tables */
table.sortable thead {
background-color:#eee;
color:#666666;
font-weight: bold;
cursor: default;
}
This is version 2 of sorttable, released April 2007. If you're using the old version, you may find it useful to update; v2 has numerous new features and should be completely backwards compatible.
--------------------------------------------------------------------------------
Advanced usage
You probably don't need the bits here. Sorttable is designed to require no configuration, no fiddling around. Just take it and use it. If you need it to do more than it does by default, then come back here and read this part.
How do I make my "totals" row stay at the bottom of my table?
How do I sort some data in a format that sorttable doesn't understand?
What do I do if sorttable isn't sorting my data the way I'd expect it to?
Can sorttable do a stable sort?
How do I make some columns not be sortable?
Can sorttable sort a table as soon as the page loads, without requiring the user to click?
How do I make sorttable sort columns in descending order first rather than ascending order?
What's new in version 2 of sorttable?
Can I use sorttable in a commercial product? On a commercial website? What licencing terms is it available under?
I love sorttable so much that I want to donate some money to you even though I don't have to. Can I?
Totals rows
If you have a "totals" row at the bottom of your table that you want to stay at the bottom of your table (and not get sorted), then add it to a <tfoot> section in your table (which is what you should be doing anyway, according to the HTML spec). So, your table should look like this:
<table class="sortable">
<thead>
<tr><th>Person</th><th>Monthly pay</th></tr>
</thead>
<tbody>
<tr><td>Jan Molby</td><td>£12,000</td></tr>
<tr><td>Steve Nicol</td><td>£8,500</td></tr>
<tr><td>Steve McMahon</td><td>£9,200</td></tr>
<tr><td>John Barnes</td><td>£15,300</td></tr>
</tbody>
<tfoot>
<tr><td>TOTAL</td><td>£45,000</td></tr>
</tfoot>
</table>
Note how sorting the table leaves the TOTAL row at the bottom.
Person Monthly pay
Steve Nicol £8,500
Steve McMahon £9,200
John Barnes £15,300
Jan Molby £12,000
TOTAL £45,000
Using custom sort keys
You may have some data which does go in an order but isn't identified by sorttable. The way to fix this problem is to use custom sort keys. Take, for example, a column of spelled out numbers. Ordinarily, sorttable wouldn't work here; it will treat the spelled-out numbers as strings, and so would sort the numbers in alphabetical order, i.e., five, four, one, three, two. To get around this, you can specify on a cell in your table a sorttable_customkey attribute, and sorttable will use the contents of that attribute instead of the text in the cell itself when sorting the table. So, for example, your table might look like this:
<table class="sortable">
<tr><th>Number (spelled)</th><th>Person</th></tr>
<tr><td sorttable_customkey="2">two</td><td>Jan</td></tr>
<tr><td sorttable_customkey="3">three</td><td>Bruce</td></tr>
<tr><td sorttable_customkey="1">one</td><td>Steve</td></tr>
</table>
Note that clicking on the "Number (spelled)" column in the table below sorts it in the correct one,two,three order. Number (spelled) Person
one Steve
two Jan
three Bruce
You can fix practically any problem you have with sorttable's automatic column typing by specifying custom sort keys.
Manually specifying a column's type
Sorttable works out the type of your columns in order to work out how to sort them (numbers sort differently than letters, for example). Occasionally, it might get it wrong. If so, you can explicitly specify a type for a column, which will override sorttable's assessment. To specify a type, add a class of sorttable_columntype to the header row of that column. Available column types are numeric, alpha, ddmm, and mmdd. The latter two are for dates, but are not likely to be useful because if sorttable fails to automatically identify a date then the sort won't work anyway.
So, for example, if you have a "part number" column which you want to be treated as if it were numeric, then you might do your table like this:
<table class="sortable">
<tr>
<th class="sorttable_numeric">Part number</th><th>Part name</th>
</tr>
<tr>
<td>111-A5</td><td>Three-eighths Gripley</td>
</tr>
<tr>
<td>31337-H4X0R</td><td>Computer system intrusion toolkit</td>
</tr>
</table>
Remember: you probably do not need to do this. It is unlikely that you'll need to "force" sorttable to recognise a column type. You may also want to investigate using custom sortkeys, above, as a better way of achieving your goals.
Stable sorting
Sorttable, by default, does an unstable sort. This means that it does not maintain the order of rows where those rows have the same key in the sorted column. Wikipedia has more on stable sorting. If this is a problem, and you need the sort to be stable, you can do it by making a tiny edit to sorttable.js. Edit the file and find the lines:
/* If you want a stable sort, uncomment the following line */
//sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
/* and comment out this one */
row_array.sort(this.sorttable_sortfunction);and change them so the shaker_sort line is uncommented instead:
sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
//row_array.sort(this.sorttable_sortfunction);The sort will now be stable. However, it will be considerably slower (the stable sorting can take eight times as long as the unstable sort), which is why it isn't enabled by default.
Making some columns unsortable
If you'd like some column headers to not be clickable, meaning that your users won't be able to resort the table by those columns, then add class="sorttable_nosort" to the <th> column header. Observe how the "number of legs" column here isn't sortable.
Name 6 Team Number of legs
Ardiles, Ossie Tottenham Hotspur 2
Charlie Nicholas Arsenal 2
Hughes, Mark Manchester Utd 2
Molby, Jan Liverpool 2
Nicol, Steve Liverpool 2
Sorting the table when the page is loaded
Lots of people ask, "how do I make sorttable sort the table the first time the page is loaded?" The answer is: you don't. Sorttable is about changing the HTML that is served from your server without a page refresh. When the page is first served from the server, you have to incur the wait for it to be served anyway. So, if you want the table sorted when a page is first displayed, serve the table in sorted order. Tables often come out of a database; get the data from the database in a sorted order with an ORDER BY clause in your SQL. Any solution which involves you running sorttable as soon as the page loads (i.e., without user input) is a wrong solution.
Sort in descending order first
Making sorttable sort your columns in descending order first rather than ascending order requires editing sorttable.js. Find the line:
row_array.sort(this.sorttable_sortfunction);and after it, add a new line:
row_array.reverse();Changes from sorttable v1
Sorttable has changed quite a lot in this new version 2. Please read the release announcement for more details of what's changed and who is to be thanked. If for some reason you need the original version 1 of sorttable you can download it from sorttable_v1.js along with the original instructions.
Licencing
Sorttable, like all my DOM scripts, is under the X11 licence, which basically means you can do what you want with it, including using it at work or in a commercial setting or product. If you're not sure, or you have other questions, please contact me for details.
Can I give you money for sorttable? I really like it
You don't have to. I believe in Free Software; writing this and seeing people use it is its own reward. Of course, if you're determined to give me money then I'm not going to stop you. You can send money to my PayPal account with the button below.
分享到:
相关推荐
<title>JavaScript表格排序 table { width: 100%; border-collapse: collapse; } th, td { text-align: left; padding: 8px; border: 1px solid #ddd; } th { cursor: pointer; } ...
总结起来,这个JavaScript表格排序示例展示了如何通过监听表头点击事件,使用纯JavaScript实现表格数据的动态排序。它依赖于HTML表格结构,通过JavaScript操作DOM节点进行数据比较和位置交换。在实际项目中,可以...
总的来说,JavaScript中的`sort()`方法和`localeCompare()`函数是实现表格排序的关键工具,但需要配合自定义的比较函数和数据转换策略,以适应不同类型的表格数据。理解这些概念并正确应用它们,可以帮助我们构建出...
下面我们将深入探讨如何使用JavaScript实现表格排序功能。 一、HTML表格基础 在HTML中,`<table>`元素用于创建表格,`<tr>`表示表格行,`<th>`表示表头单元格,`<td>`表示数据单元格。例如: ```html 姓名 ...
总的来说,sorttable是一个轻量级且强大的工具,它简化了在网页中实现表格排序的过程,提升了用户的交互体验。通过灵活的配置和良好的兼容性,sorttable能够适应多种项目需求,是前端开发者在处理表格数据排序时的...
JavaScript表格排序是一个在网页开发中常见的需求,尤其是在处理大量数据时。这个功能强大的JavaScript库,被称为"sorttable",能够帮助开发者轻松实现表格数据的排序,同时提供了自定义排序规则的能力,大大增强了...
这个轻量级的JavaScript表格排序实现非常实用,特别适合小型项目或对性能要求高的场景。它可以避免引入大型库的开销,同时也易于理解和维护。当然,对于更复杂的需求,例如处理日期或对象排序,可能需要进一步扩展这...
在JavaScript编程中,实现带箭头的表格排序是一种常见的需求,尤其在数据展示和交互式网页设计中。本文将深入探讨如何使用JavaScript实现这一功能,包括理解基础的HTML表格结构、CSS样式设置以及JavaScript事件处理...
在今天的Web开发环境中,开发者可能会选择使用更现代的解决方案,如使用Vue.js、React或Angular等前端框架,结合数据绑定和虚拟DOM技术来实现表格排序。 为了使用`sorttable`,开发者需要将库的JavaScript和CSS文件...
这是整个脚本的核心对象,它包含了所有用于实现表格排序的方法和数据。 - **oTable**: 存储页面上所有表格DOM元素的对象。 - **cellStatus**: 记录每个表格的列排序状态。 - **sortCells**: 已经添加了排序功能的...
总之,sorttable库为前端开发者提供了一个简单、高效的表格排序解决方案,其拖拽排序功能进一步提升了用户体验。无论是在小型项目还是大型应用中,适当地利用sorttable都能提高数据管理界面的易用性和效率。
在JavaScript中实现表格点击排序是常见的前端开发技巧,尤其在数据展示和管理的...通过结合HTML、CSS和JavaScript,我们可以创建出动态且用户友好的表格排序功能,这对于任何涉及数据展示的Web应用程序都是至关重要的。
在"sorttable"和"scrolltable"这两个标签所指的库中,它们通常提供了预封装的解决方案,简化了表格排序和表头浮动的实现。开发者只需引入相应的JavaScript库,按照文档配置,即可快速为自己的表格添加这些功能。例如...
在JavaScript编程中,`sortTable` 是一个常见用于对HTML表格数据进行排序的功能。这个功能主要依赖于原生JavaScript的方法来实现,使得用户可以按照指定列的值对表格中的行进行升序或降序排列。在本文中,我们将深入...
要实现“漂亮的html表格排序实现”,我们需要借助JavaScript或者jQuery等库来增强HTML表格的功能,让数据可以按照用户的需求进行升序或降序排列。下面将详细介绍这个过程中的关键知识点。 1. HTML基础 首先,我们...
总结来说,实现JavaScript表格排序并高亮显示的关键在于理解DOM操作、数组排序以及CSS样式的应用。通过编写比较函数、遍历和排序表格行,以及更新单元格样式,我们可以创建出具有动态排序和高亮功能的表格。这个过程...
在JavaScript(JS)中,实现表格排序功能是前端开发中常见的需求,特别是在处理大量数据时。这个功能使得用户可以通过点击表头对数据进行升序或降序排列,提高数据查看和分析的效率。下面我们将详细探讨如何实现这样...
通过结合jQuery和JavaScript的内置方法,我们可以轻松地实现一个功能完备且支持中文的表格排序功能。这不仅提高了用户体验,也为数据展示提供了更多灵活性。实际应用中,还可以根据项目需求进一步定制和扩展此功能。
总结来说,`sorttable.js`是一个简单易用的工具,它使得在网页上实现交互式的表格排序变得轻松。通过合理的配置和自定义,可以满足不同场景下的排序需求。对于那些对性能要求不高,但需要增强用户体验的项目,`...
在JavaScript编程中,实现表格数据...总之,JavaScript实现表格排序涉及事件处理、数据操作和DOM操作,熟练掌握这些技巧对于构建交互性强的Web应用至关重要。通过不断实践和优化,可以创造出高效、易用的表格排序功能。