Created
March 12, 2014 02:33
-
-
Save jmaddington/9499694 to your computer and use it in GitHub Desktop.
Automatically sorts jQuery datatables based on atributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 <[email protected]> | |
*/ | |
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