Monday, July 11, 2011

Table Date Column Sort with Jquery

Some time we want to sort date filed in a Table with Jquery.
Here I add a parser in ready function.

$(document).ready(function () {
            $.tablesorter.addParser({
                id: 'dateMS',
                is: function (s) {// return false so this parser is not auto detected
                    return false;
                },
                format: function (s) {
                    var mydate = s; // here my date Formate is yyyy-MM-dd we know that javaScript default format is MM/dd/yyyy so I am converting date filed in a (MM/dd/yyyy) format.
                    var datecal = mydate.split('-')[1].toString() + "/" + mydate.split('-')[2].toString() + "/" + mydate.split('-')[0].toString();
                    var d = Date.parse(datecal);
                    if (isNaN(d)) {
                        return -1;
                    }
                    return d;
                },
                // set type, either numeric or text
                type: 'numeric'
            });

            $("#TblActiveCases").tablesorter({ sortList: [[0, 0]], textExtraction: "complex", widthFixed: false, dateFormat: 'YYYY-mm-dd',
                headers: { 1: { sorter: 'dateMS' }, 2: { sorter: false} }

            })

        });

No comments:

Post a Comment