Angular-Datatables在日期排序错误

Mor*_*p25 4 javascript jquery datatables angularjs angular-datatables

我正在我的项目中使用angular-datatables插件,除日期外,它适用于所有类型.

示例DESC:

  • 2016年1月1日
  • 2015年1月8日
  • 2015年1月8日
  • 2015年1月9日

示例ASC:

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

我在ng-repeat中使用Angular Way和日期过滤器.我怀疑它是用错误的日期格式排序的.我想根据当天排序.我怎样才能解决这个问题?

<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>
Run Code Online (Sandbox Code Playgroud)

dav*_*rad 13

dataTable通常通过检测每列的数据类型来做得很好.但是,如果类型检测遇到与例如假定数字冲突的任何内容,则该列将变为默认的alpha排序.我坚信这就是这种情况 - 如果渲染的内容符合dd/MM/yyyy100%的标准,那么dataTables应该自动将该列排序为date.

幸运的是,我们可以date通过columns/ columnDefssettings 强制数据类型.用例如DTColumnDefBuilder:

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

这迫使列3,4,5和11成为类型date.包含dtColumnDefs在标记中:

<table class="table table-hover" datatable="ng" dt-column-defs="dtColumnDefs">
Run Code Online (Sandbox Code Playgroud)

示例 - 尝试注释掉.withOption('type', 'date')并查看差异 - > http://plnkr.co/edit/XpBcLhlm0Frq3voN6X97?p=preview