Cannot sort on uk-date

Cannot sort on uk-date

aramearame Posts: 3Questions: 1Answers: 0
edited February 2015 in Free community support

I am using datatables 1.9.4 and jquery 1.9.1. Maybe I should upgrade to 1.10?

Anyway my main question is this.

I am working on a legacy application where I want to sort in descending order the date field.

I have the sort functions in place:

    jQuery.fn.dataTableExt.oSort['uk_date-asc'] =
        function (a, b) {
            var ukDatea = a.split('/'); var ukDateb = b.split('/');
            var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
            var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        };

     jQuery.fn.dataTableExt.oSort['uk_date-desc'] = function (a, b) {
         var ukDatea = a.split('/'); var ukDateb = b.split('/');
         var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
         var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
         return ((x < y) ? 1 : ((x > y) ? -1 : 0));
     };

My table is joined up by:

     $('#EmpSearchSummary').dataTable({
         "bJQueryUI": false,
         "iDisplayLength": 25,
         "sPaginationType": "full_numbers",
         "bSort": true,
         "aoColumns": [
         { "sType": "uk_date-desc" }, null, null, null,null
        ]
     });

and my table looks like

 <table id ="EmpSearchSummary"  style="width: 800px;" class="display">
     <thead>
         <tr>
             <th colspan="5" style="font-size: 15px; text-align: center; text-transform: uppercase; font-family: Verdana">
                 Your Latest Timesheets List
             </th>
         </tr>
         <tr>   
             <th>
                 Weekend Date                    
             </th>
             <th>
                 Timesheet Status                    
             </th>
             <th>
                 Date Submitted                    
             </th>
             <th>
                 Total Hours                    
             </th>
             <th>
                 Details                    
             </th>              
         </tr>
     </thead>
     <tbody >
         @foreach (var historyItem in Model.TSVMList.OrderByDescending(x => x.TSheet.WeekID))
         {
             <tr class="history">
                 <td>@Html.DisplayFor(x => historyItem.TSheet.Week.WeekEndDate)</td>
                 <td>@Html.DisplayFor(x => historyItem.TSheet.TimeSheetStatu.TimesheetStatusText)</td>
                 <td>@Html.DisplayFor(x => historyItem.TSheet.Date_Submitted)</td>
                 <td>@Html.DisplayFor(x => historyItem.TSheet.TotalHrsWorked)</td>
                 <td>@Html.ActionLink("Details", "EmployeeSearchDetail", new { id = @historyItem.TSheet.TimesheetID })  </td>
             </tr>
         }
     </tbody>
 </table>

However I am getting this runtime error;

JavaScript runtime error: Object doesn't support property or method 'uk_date-desc-asc'

What am I doing wrong and how do I fix this?

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Assuming you're using the date-uk plug-in, I would take a look at this as the use of that plug-in is deprecated:

    http://datatables.net/plug-ins/sorting/date-uk

    That page explains the preferred method for uk date sorting, although you might need to upgrade your DataTables.

  • aramearame Posts: 3Questions: 1Answers: 0

    Because this is a legacy app, I am a bit wary about upgrading jquery libraries. However I think the issue I want to address is how do I make the default sort direction to be descending? One hack solution is to rename the functions to do the opposite of what they say, but that would be confusing.

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    how do I make the default sort direction to be descending?

    Use the asSorting option to control that. You may also need to specify aaSorting if it is the column that is sorted when DataTables initialises.

    Allan

This discussion has been closed.