views:

37

answers:

2

I am using DataTable Plugin for html table sorting. It is working fine. Now I have a column with Dates(d/m/Y) in textboxes. Sorting on that column is not working. I want to make this sortable by date.

First I applied the following code to make textbox sortable.

Table with 3 fields:

<script type='text/javascript' charset='utf-8'>

                $.fn.dataTableExt.afnSortData['dom-text'] = function  ( oSettings, iColumn ) {
                    var aData = [];
                    $( 'td:eq('+iColumn+') input', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
                        aData.push( this.value );
                    } );
                    return aData;
                }


                $(document).ready(function() {
                    $('#sortableTable').dataTable( {
                        'aoColumns': [null, null,{ 'sSortDataType': 'dom-text' }]
                    } );
                } );


</script> 

Date sorting is working fine on date column without text fields.

How to apply date sorting on date in textbox.

EDIT:

There are following functions in jquery.dataTables.js

"date-asc": function ( a, b )
        {
            var x = Date.parse( a );
            var y = Date.parse( b );

            if ( isNaN(x) || x==="" )
            {
            x = Date.parse( "01/01/1970 00:00:00" );
            }
            if ( isNaN(y) || y==="" )
            {
                y = Date.parse( "01/01/1970 00:00:00" );
            }

            return x - y;
        },

        "date-desc": function ( a, b )
        {
            var x = Date.parse( a );
            var y = Date.parse( b );

            if ( isNaN(x) || x==="" )
            {
            x = Date.parse( "01/01/1970 00:00:00" );
            }
            if ( isNaN(y) || y==="" )
            {
                y = Date.parse( "01/01/1970 00:00:00" );
            }

            return y - x;
        },
A: 
<script type='text/javascript' charset='utf-8'>

                $.fn.dataTableExt.afnSortData['dom-text'] = function  ( oSettings, iColumn ) {
                    var aData = [];
                    $( 'td:eq('+iColumn+') input', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () {
                        aData.push( this.value );
                    } );
                    return aData;
                }


                $(document).ready(function() {
                    $('#sortableTable').dataTable( {
                        'aoColumns': [null, null,{ 'sSortDataType': 'dom-text', 'sType': 'date' }]
                    } );
                } );


</script> 
Awan
+1  A: 

The manual of the DataTable has a page on exactly that subject, perhaps it can help you find an answer to your question.

Jasper