我有一个来自 API 的日期列表,其中一个日期类似于"2017-07-05T10:53:24.000Z">
我在数据表的第一列中显示日期。在控制器中,我定义列,例如
$scope.dtColumnDefs = [
DTColumnDefBuilder.newColumnDef(0).withOption('type', 'date'),
DTColumnDefBuilder.newColumnDef(1).notSortable(),
....
];
.HTML
<table datatable="ng" dt-options="dtOptions" dt-column-defs="dtColumnDefs">
<thead>
<tr class="table-header-color-yellow">
<th class="table-head-style">Date</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="review in reviews">
<td>{{review.date | date}}</td>
</tr>
</tbody>
</table>
我如何按降序对日期进行排序,因为它目前按升序排序?
只需设置order
选项:
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('order', [[0, 'desc']])
您对columnDefs
的使用有点错误。它应该是
$scope.dtColumnDefs = [
DTColumnDefBuilder.newColumnDef(0).withOption('type', 'date').notSortable()
];
将日期部分转换为dd/MM/yyyy
然后在控制器使用
$scope.dtColumnDefs = [
DTColumnDefBuilder.newColumnDef([1]).withOption('type', 'date')
];
HTML应该是这样的
<table class="table table-hover" datatable="ng" dt-column-defs="dtColumnDefs">