This is a simple script written as a plug-in, enabling page authors to make their HTML tables sortable without needing to know any JavaScript syntax.
By default any eligible table will be made sortable, without the page author having to add or change any special attributes on the table elements. Special attributes can be added in case the page author wants specific tables or columns to be unsortable.
A table is eligible if it has one or more "header rows": rows consisting purely of column headers (<th>
elements).
The script will add button behaviour to these, for initiating sorting. The script will automatically update aria-sort
attribute values of column headers, and this can be used as a CSS selector for styling sort indicators.
Portrait | Name | Health | Weapon | Nation | Birthday | Voice | Score |
---|---|---|---|---|---|---|---|
Amber | 793 | Bow | Mondstadt | August 10 | 蔡书瑾 | ☆☆☆☆★ | |
Xiangling | 911 | Polearm | Liyue | November 2 | 小N | ★★★★★ | |
Sucrose | 775 | Catalyst | Mondstadt | November 26 | 小敢 | ★★★★★ | |
Sayu | 993 | Claymore | Inazuma | October 19 | Sakula小舞 | ☆★★★★ | |
Dori | 1039 | Claymore | Sumeru | December 21 | 王晓彤 | ☆☆☆★★ | |
Barbara | 820 | Catalyst | Mondstadt | July 5 | 宋媛媛 | ☆☆☆★★ | |
Collei | 820 | Bow | Sumeru | May 8 | 秦文静 | ☆☆☆★★ | |
Yun Jin | 893 | Polearm | Liyue | May 21 | 贺文潇 | ☆☆★★★ | |
Diona | 802 | Bow | Mondstadt | January 18 | 诺亚 | ☆★★★★ |
The script will automatically try to recognize data types such as numbers and dates, but will otherwise just sort according to character order (e.g. alphabetical)
alt
text.<th>
elements, but they are treated as data cells whenever they share a row with <td>
elements.<th>
elements should function as sort trigger.<td>
elements should be sortable.data-sortvalue="x"
(replace x
with whichever value you want) can be added to <td>
elements to override the cell's content for sorting. For example: if the script thinks "100kg" is lighter than "20g", you can use <td data-sortvalue="100000">100kg</td>
and <td data-sortvalue="20">20g</td>
instead, and the script will sort by the attribute values.data-sortable="false"
can be added to <table>
and <th>
elements to prevent tables and columns from being sortable.<script>
element (is it possible to make set defer from withing the script itself?).