Sortable Tables – with uncommon date format fix

Sortable Tables via script: Sorttable

For the different date formats that are used in many countries; with and without dots, with different month and weekday names, I use a small script to create sortable date formats for the above sorttable script and add them as attributes to the date fields: moment.js

For localisation download the file from: Locales
and upload that to the same folder as your js files.

So that you can recognise that there are sortable table columns when loading the page, I trigger a first click after loading.

function include_custom_js() {
	wp_enqueue_script( 'kryogenix-sorttable', get_stylesheet_directory_uri().'/js/sorttable.js', array('jquery'), 2, true );
	wp_enqueue_script( 'moment', get_stylesheet_directory_uri().'/js/moment.js', array('jquery'), 2, true );
	wp_enqueue_script( 'moment-de', get_stylesheet_directory_uri().'/js/de.js', array('jquery'), 2, true );

	// if you need the austrian "Jänner" f.e:
	wp_enqueue_script( 'moment-de-at', get_stylesheet_directory_uri().'/js/de-at.js', array('jquery'), 2, true );
}
add_action( 'wp_enqueue_scripts', 'include_custom_js', 100 );
function fix_for_sorttable(){
?>
<script>
window.addEventListener("DOMContentLoaded", function () {  
(function($){
    $('.avia-table.sortable').each(function(){
      // this is the heading of the column with dates - you had to adjust to your needs
      var dateTableIndex =  $(this).find('thead th:contains("Dat")').index();
      var dateChildNumber = parseInt(dateTableIndex)+1;
      $(this).find('tbody td:nth-of-type('+dateChildNumber+')').addClass('date');
    });
           
    $(".date").each(function(idx) {
        // change the locale setting to your needed language - spcifications may occur like de-at - for austria 
        // i really don't know if this is needed - because ewen without it - only with the loading of de-at it works.
        moment.locale('de');
        var datum = $(this).text(),
        datum_formated = moment( datum , 'DD. MMMM YYYY').format('YYYY-MM-DD');
        $(this).attr("sorttable_customkey", datum_formated);
    });  
	
	setTimeout(function () {
		$('.avia-table.sortable').find('.avia-heading-row th:first-child').trigger('click');
	},1000); 
	
})(jQuery);
});
</script>
<?php
}
add_action('wp_footer', 'fix_for_sorttable');

custom class to table element for scrollable table:  scroll

DatumName
17. April 1968Karl Heinz
28. Juli 1992Max
25. September 2001Paula
16. Mai 1962Günter
25. April 1932Hermann
02. März 1978Hanna
17. Oktober 1984Liam
01. Januar 1998Gunther
01. Jänner 1998Günter2