Skip to content

Instantly share code, notes, and snippets.

@jmaddington
Created March 12, 2014 02:33
Show Gist options
  • Save jmaddington/9499694 to your computer and use it in GitHub Desktop.
Save jmaddington/9499694 to your computer and use it in GitHub Desktop.
Automatically sorts jQuery datatables based on atributes
/*
Autosorting function for jQuery datatables. Looks for a table with the classes datatable and autosort
If it finds thead>tr>th[data-autosort] and then sorts the table by that TH's index in the direction indicated
by the data-sortdir attribute.
@author JM Addington <jm@jmaddington.com>
*/
function autosort() {
$('.datatable.autosort').each(function() {
var n = 0;
parent = this;
$(this).find('thead').find('tr').children().each(function() {
if ($(this).attr('data-autosort') == 'true') {
$(parent).dataTable().fnSort( [[n, $(this).attr('data-sortdir')]]);
}
n++;
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment