User:TimR/Tables

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:
 * (however, this more cumbersome method is less often needed after the described fixes are applied)
 * (however, this more cumbersome method is less often needed after the described fixes are applied)
 * (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 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:

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.

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:

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 template. The usage is just the same as the 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:

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