Display date in descending order in AngularJS data

2019-08-07 06:05发布

I have a list of dates coming from API and one such date is like "2017-07-05T10:53:24.000Z"

Am displaying dates in first column in datatable. In controller am defining the column like

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

How can i sort the dates in descending order as it is sorting in ascending order as of now?

2条回答
2楼-- · 2019-08-07 06:15

Just set the order option :

$scope.dtOptions = DTOptionsBuilder.newOptions()
  .withOption('order', [[0, 'desc']])

Your usage of columnDefs is a little bit wrong. It should be

$scope.dtColumnDefs = [
  DTColumnDefBuilder.newColumnDef(0).withOption('type', 'date').notSortable()
];  
查看更多
我命由我不由天
3楼-- · 2019-08-07 06:22

Convert date part into dd/MM/yyyy

Then in controller use

$scope.dtColumnDefs = [  
    DTColumnDefBuilder.newColumnDef([1]).withOption('type', 'date')
];

and Html should be like this

<table class="table table-hover" datatable="ng" dt-column-defs="dtColumnDefs">
查看更多
登录 后发表回答