User:TimR/Tables

From Wikipedia, the free encyclopedia

This page gives some examples of my extensions to sortable tables. Currently, sorting is done on the content of table cells, which is a good start, but not very flexible. The current guidance illustrates a workaround by using non-displayed text:

The most versatile is alphabetic sorting using a sortkey which due to CSS is not displayed:
<span style="display:none">...</span>
(however, this more cumbersome method is less often needed after the described fixes are applied)

but this is a little clumsy to use.

I have adapted the table sorting code slightly to allow a sorting key for a cell to be defined anywhere in that cell. To see this in action, you need to edit your monobook.js file to include the following line:

importScript('User:TimR/sorttable.js');

Without it, the following tables will still be sortable, but will sort according to the default rules. With this line added to your configuration, you will be able to see these new sorting mechanisms.

Specifying simple keywords

Provided that the word or phrase which you wish to use to sort on is already in a cell, you can simply mark it with the {{sortkey}} template. It will still get displayed just as it would without the template, but its value will be used to sort the table. For example, it can be used to mark a person's surname so that the column will sort on that:

{| class="wikitable sortable"
!Astronomer Royal!!From!!To
|-
|Rev'd John {{sortkey|Flamsteed}}||1675||1719
|-
|Professor Edmond {{sortkey|Halley}}||1720||1742
|-
|Dr James {{sortkey|Bradley}}||1742||1762

...

|Martin {{sortkey|Rees}}, Baron Rees of Ludlow||1995||&nbsp;
|-
|}

Of course, the table still looks the same, but it sorts properly. Here is the example. (Remember that it will not sort properly unless you have edited your monobook.js file.) Try clicking on the icon at the top of the first column to see the sort by surname.

More information Astronomer Royal, From ...
Astronomer RoyalFromTo
Rev'd John 16751719
Professor Edmond 17201742
Dr James 17421762
Nathaniel 17621764
Rev'd Nevil 17651811
John 18111835
Sir George Biddell 18351881
Sir William 18811910
Sir Frank 19101933
Sir Harold Spencer 19331955
Professor Sir Richard 19561971
Professor Sir Martin 19721982
Professor Sir Francis 19821990
Professor Sir Arnold 19911995
Martin , Baron Rees of Ludlow1995 
Close

Specifying more complex content

In many cases, you want more than just plain text in a table cell, such as links. You may also want to specify a sort key for this content. This is where a sort key and other markup might come into conflict.

If you simply want to specify the text of a link as the sort key, you simply make the link inside the template, as shown here:

{| class="wikitable sortable"
!Date!!Day!!Present
|-
|{{hiddenkey|20070105}}[[5 January]] [[2007]]
|12||twelve {{sortkey|[[drummers]]}} drumming
|-
|[[4 January]] [[2007]]{{hiddenkey|20070104}}
|11||eleven {{sortkey|[[pipers]]}} piping

...

|{{hiddenkey|20061225}}[[25 December]] [[2006]]
|1||a {{sortkey|[[partridge]]}} in a pear tree
|-
|}

Note that in the case of piped links, the displayed text acts as the sort key, not the article name.

With this table, we also want to be able to sort on the first column by date. In this case we need to specify a sort key which can be used to sort the rows in the right order, but doesn't get displayed. We do that with the {{hiddenkey}} template. The usage is just the same as the {{sortkey}} template: it can come anywhere in the cell and you simply specify the sort key, but the key itself is invisible. I have demonstrated this by putting it in different places within the cell in various rows in the table:

{| class="wikitable sortable"
!Date!!Day!!Present
|-
|{{hiddenkey|20070105}}[[5 January]] [[2007]]
|12||twelve {{sortkey|[[drummers]]}} drumming
|-
|[[4 January]] [[2007]]{{hiddenkey|20070104}}
|11||eleven {{sortkey|[[pipers]]}} piping

...

|{{hiddenkey|20061225}}[[25 December]] [[2006]]
|1||a {{sortkey|[[partridge]]}} in a pear tree 
|-
|}

So here is the final product. Try playing with the columns to see how it sorts.

More information Date, Day ...
DateDayPresent
5 January 2007 12twelve drumming
4 January 2007 11eleven piping
3 January 2007 10ten a-leaping
2 January 2007 9nine dancing
1 January 2007 8eight a-milking
31 December 2006 7seven a-swimming
30 December 2006 6six a-laying
29 December 2006 5five
28 December 2006 4four
27 December 2006 3three
26 December 2006 2two
25 December 2006 1a in a pear tree
Close

Related Articles

Wikiwand AI