Display date in descending order in AngularJS data

2019-08-07 06:14发布

问题:

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?

回答1:

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()
];  


回答2:

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