简体   繁体   中英

Issue with sorting jquery datatables

It appears to sort fine at first glance, for example it sorts from 2019 dates all the way to 2020, then goes back to 2019 and on wards. (Date format DD/MM/YYYY). Have included the script and the table code below. Can someone help?

My code is as follows...

     <script type="text/javascript">
                $(document).ready(function () {
                jQuery.extend( jQuery.fn.dataTableExt.oSort, {
                "date-uk-pre": function ( a ) {
                    var ukDatea = a.split('/');
                    return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
                },
        
                "date-uk-asc": function ( a, b ) {
                    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
                },
        
                "date-uk-desc": function ( a, b ) {
                    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
                }
                } );
        
                    $('#tbl').DataTable({
                        stateSave: true,
                        order: [[9, 'asc']],
                        "aoColumns": [
                            null,
                            null,
                            null,
                            null,
                           {  "bSortable": false },
                            { "sType": "date-uk" },  //THIS IS THE ITEM I CLICK THE SORT BUTTON ON.
                            null,
                            null,
                            null,
                            { "sType": "date-uk" },
                             { "sType": "date-uk" },
                            null],
                      paging: false, "oLanguage": {
                        "sEmptyTable": "",
                        "sSearch": ""
                    }
                    });
        </script>

// Table code

    <table class="table  table-bordered" id="tbl">
    <thead>
        <tr>
            
            <th>
               Test Date sort 
            </th>
    
        </tr>
    </thead>

<tbody>
        <td>
            @Html.DisplayFor(modelItem => item.TestDate)
        </td>

</tbody>
</table>

Thanks for all inputs, here's the solution:

Changed date format to:

@Convert.ToDateTime(item.TestDate).ToString("yyyy/MM/dd")

Instead of dd/mm/yyyy. Then removed the following code from the jquery:

//jQuery.extend( jQuery.fn.dataTableExt.oSort, {
//"date-uk-pre": function ( a ) {
//    var ukDatea = a.split('/');
//    return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
//},

//"date-uk-asc": function ( a, b ) {
//    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
//},

//"date-uk-desc": function ( a, b ) {
//    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
//}
//} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM