简体   繁体   中英

Angular-Datatables wrong sorting on date

I'm using the angular-datatables plugin in my project, which works fine on all types, except for dates.

Example DESC:

  • 01/01/2016
  • 01/08/2015
  • 01/08/2015
  • 01/09/2015

Example ASC:

  • 31/12/2015
  • 31/10/2015
  • 22/10/2015

I'm using the Angular Way with a date filter in my ng-repeat. I have a suspicion that it sorts with a wrong date format. I would like it to sort based on the day. How can I fix this?

<table class="table table-hover" datatable="ng">
        <thead>
            <tr>
                <th>Client</th>
                <th>Project</th>
                <th>ID</th>
                <th>Inv. Date</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>DKK ex VAT</th>
                <th>CIG</th>
                <th>Attention</th>
                <th>Cust. Manager</th>
                <th>Regarding</th>
                <th>Due Date</th>
                <th>Finalized</th>
                <th>Paid</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="invoice in vm.latestInvoices.LatestInvoices">
                <td>{{invoice.CompanyName}}</td>
                <td>{{invoice.ProjectName}}</td>
                <td>{{invoice.InvoiceID}}</td>
                <td>{{invoice.InvoiceDate | date: 'dd/MM/yyyy'}}</td>
                <td>{{invoice.InvoiceStart | date: 'dd/MM/yyyy'}}</td>
                <td>{{invoice.InvoiceEnd | date: 'dd/MM/yyyy'}}</td>
                <td>{{invoice.DKKexVAT}}</td>
                <td>{{invoice.CustomerInvoiceGroup}}</td>
                <td>{{invoice.Attention}}</td>
                <td>Customer Manager</td>
                <td>{{invoice.Regarding}}</td>
                <td>{{invoice.DueDate | date: 'dd/MM/yyyy'}}</td>
                <td>No</td>
                <td>{{invoice.Paid}}</td>
            </tr>
        </tbody>
    </table>

dataTables generally does a good job by detecting datatypes for each column. However, if the type detection meets anything that conflicts with for example assumed numbers, the column is turned into default alpha sorting. I strongly believe this is the case here - if the rendered content meets the dd/MM/yyyy criteria 100%, then dataTables should automatically sort that column as date .

Luckily we can force the date datatype through the columns / columnDefs settings. Use for example DTColumnDefBuilder :

$scope.dtColumnDefs = [  
    DTColumnDefBuilder.newColumnDef([3,4,5,11]).withOption('type', 'date')
];

This forces column 3,4,5 and 11 to be of type date . Include dtColumnDefs in the markup :

<table class="table table-hover" datatable="ng" dt-column-defs="dtColumnDefs">

Example - try to comment out the .withOption('type', 'date') and see the difference -> http://plnkr.co/edit/XpBcLhlm0Frq3voN6X97?p=preview

Though it's late, but there is an alternative approach for those who want more flexibility.

You can solve the sorting problem in the way mentioned in the Ultimate Date / Time sorting plugin .

The code is really simple:

jQuery.fn.dataTable.moment('DD/MM/YYYY');

You can import the plugin from the datatable CDN:

  <script src="https://cdn.datatables.net/plug-ins/1.10.13/sorting/datetime-moment.js"></script>


Note: You'll need to include momentJs in your application in order to use the plugin.

I solved this issue creating a filter

daterFilter.js

angular.module("app").filter("dateFilter", function(){
  return function(input) {
    var o = input.replace(/-/g, "/"); // Replaces hyphens with slashes if dd/mm/YYYY format
    return Date.parse(o + " -0000");
  };
});

In html file

<span>[[ ::created_at | dateFilter | date:"dd/MM/yyyy HH:mm"]]</span>

I saw this answer some time ago in this stack:

AngularJS: How to format ISO8601 date format?

this.dataSource.sortingDataAccessor = (item, property) => {
    switch (property) {
      case 'creationDate': return parse(item.creationDate, 'dd-MM-yyyy', new Date());
    default: return item[property];
  }
};

Add this on ngAfterViewInit method

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