On the DataTables blog you will find news, announcements and tutorials about DataTables and its suite of extensions. New posts are published monthly, with additional news items in between.
Let's stereotype for a moment: developers whose first (and in many cases, such as my own, only) language is English, don't always appreciate localisation issues to the degree we should. With people using DataTables all over the world (68 community translations for DataTables core) this is something that I'm very aware of as a library developer.
Also, don't make the mistake of thinking that this post is only for developers who are working in languages other than English - the sorting options made available by the
Intl API offer something for everyone!
If you're googling for DataTables or any other keyword you'll notice that the search keyword will be highlighted in the description of each result. This behaviour allows users to find what they're searching for more faster and therefore leads to a better usability.
DataTables itself offers a search feature that filters a table to only show relevant rows. But it doesn't offer built-in keyword highlighting within these results. If you'd like to improve the usability for your users too, keep on reading.
The ordering of information in a DataTable is probably the aspect that I cover most in this blog (enums and dates for example, and more to come), but that is because it such a rich topic! DataTables is used in a wide range of applications from space telescope telemetry to amateur football leagues, so it needs to be able to cope with lots of different data types. DataTables core ships with just a basic set of data sorting functions (string, number, currency, percentage, ISO8601 dates) so, for anything outside of this range DataTables provides a sorting plug-in API that you can use to define your own sorting methods.
Typically with a sorting method you would have linear ordering, whereby the descending sort is the exact reverse of ascending, and everything is ordered by a simple comparison (numeric or character code points). This post is going to be a little different: I'll introduce a non-linear configurable sorting plug-in, whereby you can keep specific pieces of data at the top or the bottom of a column, regardless of the sorting direction and the value of that item.
An example is always worth a million words, so let's consider the following table:
|Tiger Nixon||System Architect||Edinburgh||2011/04/25||$320,800|
|Ashton Cox||Junior Technical Author||San Francisco||2009/01/12||$86,000|
|Brielle Williamson||Integration Specialist||New York||2012/12/02||$372,000|
|Unknown||Sales Assistant||San Francisco||2012/08/06||TBC|
|Rhona Davidson||Integration Specialist||Tokyo||2010/10/14||$327,900|
|Sonya Frost||Software Engineer||Edinburgh||2008/12/13||$103,600|
In this case we have two columns where absolute ordering can be useful:
- Name column where there are "Unknown" entries that should be shown at the top of the table.
- Salary column where "TBC" should be shown at the top and "Not available" should be shown at the bottom.
If you alter the sorting that is applied to the table by clicking on the headers of those columns, you will be able to see that those items stay in position regardless of the sorting of the column. If sorting is applied to one of the other columns the rows will be sorted normally.