Ludvik Ctrnacty
Ludvik Ctrnacty

Reputation: 351

Tablesorter date dd.mm.yyyy

I have following table:

<table class='tablesorter'>
<thead>
<tr style='font-weight:bold;'><td></td><th>Date</th><th>Name</th><th>Town</th></tr>
</thead>
<tbody>
<tr><td>04.08.2013</td><td>Martin</td><td>Chicago</td></tr>
<tr><td>04.08.2013</td><td>Justin</td><td>Washington</td></tr>
<tr><td>04.08.2013</td><td>Paul</td><td>Berlin</td></tr>
<tr><td>04.08.2013</td><td>Penny</td><td>Prague</td></tr>
</tbody>
</table>

and I use following Tablesorter setting:

<script type="text/javascript" id="js">
 $(document).ready(function() {     
    $("table").tablesorter({
        sortList: [[1,1]]
         headers: { 
                    1: {sorter:"dd.mm.yyyy"}        
              }         
    });
    $.tablesorter.addParser({
        id: "dd.mm.yyyy",
        is: function(s) {
            return false;
        },
        format: function(s) {
            s = "" + s;
            var hit = s.match(/(\d{1,2})\.(\d{1,2})\.(\d{4})/);
            if (hit && hit.length == 4) {
                return hit[3] + hit[2] + hit[1];
            }
            else {
                return s;
            }
        },
        type: "text"
    }); 
});
</script>

It work's fine for sorting column with date in format dd.mm.yyyy, but I also need 'sorting arrows' for custom user sorting like here

Any ideas?

Thanks

P.S.: Sorry for my english :)

Upvotes: 0

Views: 2729

Answers (1)

Mottie
Mottie

Reputation: 86413

Try putting the $.tablesorter.addParser function outside of the document ready function (before initializing the table):

<script>
$.tablesorter.addParser({
    id: "dd.mm.yyyy",
    is: function(s) {
        return false;
    },
    format: function(s) {
        s = "" + s;
        var hit = s.match(/(\d{1,2})\.(\d{1,2})\.(\d{4})/);
        if (hit && hit.length == 4) {
            return hit[3] + hit[2] + hit[1];
        } else {
            return s;
        }
    },
    type: "text"
}); 
$(function() {     
    $("table").tablesorter({
        sortList: [[1,1]], // add the missing comma here!
        headers: { 
            1: {sorter:"dd.mm.yyyy"}        
        }         
    });
});
</script>

Upvotes: 4

Related Questions