---
layout: default
---
## Quick start
{% highlight html %}
{% endhighlight %}
## Sort types
See all available sort types in the [sort directory of the project](https://github.com/tristen/tablesort/tree/gh-pages/src/sorts/). Defaults to string if no sort types are provided.
## Additional options
### Ascending/Descending
You can pass an alternate sort order as a second parameter. By default sort is ascending. To change this, set:
{% highlight js %}new Tablesort(document.getElementById('table-id'), {
descending: true
});
{% endhighlight %}
**Note:** If you are using the default CSS provided you'll need to reverse the class names that style the arrows.
### Exclude columns or rows
For columns or rows that do not require sorting, you can add attribute `data-sort-method='none'` to a columns `th` or a `tr` element.
{% highlight html %}Name |
1 |
Gonzo the Great |
12-2-70 |
Radishes |
$0.63 |
{% endhighlight %}
### Override data that is sorted on
Sometimes text inside cells is not normalized. Using a `data-sort` attribute you can use optional data to sort on.
{% highlight html %}
1 |
01/08/13 @ 8:47:18am EST |
2 |
3/7/2004 @ 9:24:45 EST |
{% endhighlight %}
You can use a custom attribute (instead of `data-sort`) using the `sortAttribute` option:
{% highlight js %}
var table = document.getElementById('table-id');
var sort = new Tablesort(table, { sortAttribute: 'data-custom-sort-val'});
{% endhighlight %}
### Specify the sort method for a column
By adding a `data-sort-method` attribute to a table heading you can force Tablesort to use a specific sorting method rather than guessing it. The value of `data-sort-method` corresponds to the name of a sort function.
{% highlight html %}
Number |
Version |
1 |
3.7.2004 |
2 |
1.08.2013 |
{% endhighlight %}
### Specify which table heading row enables sorting
If you have two or more table heading rows you can specify the one that enables sorting by adding a `data-sort-method='thead'` attribute to desired `` element.
{% highlight html %}
Sort Row |
Not Sort Row |
2 |
1 |
3 |
{% endhighlight %}
### Events
Tablesort supports two custom events `beforeSort` & `afterSort`.
{% highlight js %}var table = document.getElementById('table-id');
var sort = new Tablesort(table);
table.addEventListener('beforeSort', function() {
alert('Table is about to be sorted!');
});
table.addEventListener('afterSort', function() {
alert('Table sorted!');
});
{% endhighlight %}
Name |
Born |
Roy Eldridge |
1911 |
Tim Hagans |
1954 |
Freddie Hubbard |
1938 |
### Refresh sort on appended data
Tablesort supports sorting when new data has been added. Simply call the refresh method.
{% highlight js %}var table = document.getElementById('table-id');
var sort = new Tablesort(table);
// Make some Ajax request to fetch new data and on success:
sort.refresh();
{% endhighlight %}
Name |
Born |
Roy Eldridge |
1911 |
Tim Hagans |
1954 |
Freddie Hubbard |
1938 |
Append a row
Remove a row
### Default sort on tablesort initialization
It is possible to automatically sort the table once you create a Tablesort instance by adding `data-sort-default` attribute.
{% highlight html %}Born |
{% endhighlight %}
Name |
Born |
Roy Eldridge |
1911 |
Tim Hagans |
1954 |
Freddie Hubbard |
1938 |
### Sorting by column keys
Sometimes, tables can have more complex column structures, especially when using colspans. In these cases, you can
explicitly connect a header to the cells in each row that it should sort by, by using the `data-sort-column-key`
attribute.
This example sorts products by price, even though the prices are not in the same column as their header.
{% highlight html %}
Product |
Price |
Apples |
Sale! |
20 |
Bread |
Out of stock |
10 |
Radishes |
In Stock! |
30 |
{% endhighlight %}
Product |
Price |
Apples |
Sale! |
20 |
Bread |
Out of stock |
10 |
Radishes |
In Stock! |
30 |
## CSS styling
Add the styling from [tablesort.css](../tablesort.css) file to your CSS or roll with your own.
## Licence
MIT
## Bugs?
[File Them](https://github.com/tristen/tablesort/issues)