4

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

示例 DESC:

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

示例 ASC:

  • 2015 年 12 月 31 日
  • 2015 年 10 月 31 日
  • 22/10/2015

我在我的 ng-repeat 中使用带有日期过滤器的 Angular 方式。我怀疑它以错误的日期格式排序。我希望它根据日期进行排序。我怎样才能解决这个问题?

<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>
4

3 回答 3

15

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

date幸运的是,我们可以通过columns/columnDefs设置强制数据类型。使用例如DTColumnDefBuilder

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

这会强制第 3、4、5 和 11 列的类型为date。包括dtColumnDefs在标记中:

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

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

于 2016-01-22T11:53:22.993 回答
0

虽然为时已晚,但对于那些想要更多灵活性的人来说,还有另一种方法。

您可以按照Ultimate Date/Time 排序插件中提到的方式解决排序问题。

代码非常简单:

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

您可以从数据表 CDN 导入插件:

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


注意:您需要在应用程序中包含 momentJs 才能使用该插件。

于 2017-02-17T12:40:55.403 回答
0

我解决了这个问题,创建了一个过滤器

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");
  };
});

在 html 文件中

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

我前段时间在这个堆栈中看到了这个答案:

AngularJS:如何格式化 ISO8601 日期格式?

于 2017-07-06T22:29:36.213 回答