Angular 数据表按降序日期排序不起作用

Angular datatables sort by Descending Date not working

如何在 Angular 数据表中按降序排列日期?我尝试探索所有相关问题并找到了这个

但不适合我。使用 .withOption('order', [[4, 'desc']]) 不起作用。那怎么办?

<table class="table table-striped table-bordered table-hover" datatable="ng" dt-options="dtOptions" dt-column-defs="dtColumnDefs">
  <thead>
     <tr>
        <th class="col-sm-3"> {{'Name'| translate}} </th>
        <th class="col-sm-2 text-center"> {{'Company_Name'| translate}} </th>
        <th class="col-sm-2 text-center"> {{'Email'| translate}} </th>
        <th class="col-sm-2 text-center"> {{'Mobile_No'| translate}} </th>
        <th class="col-sm-2 text-center"> {{'Created_Date'| translate}} </th>
        <th class="col-sm-1 text-center">{{'Action'| translate}}</th>
     </tr>
   </thead>
   <tbody>
   <tr class="odd gradeX" ng-repeat="x in feedbacks" ng-if="feedbacks.length > 0">
     <td>{{x.name}}</td> 
     <td class="text-center">{{x.business_name}}</td>
     <td class="text-center">{{x.email}}</td>
     <td class="text-center">{{x.phone}}</td>
     <td class="col-sm-2 text-center" style="font-weight:normal"> {{x.created_date| date:'dd/MM/yyyy hh:mm a'}} </td>
     <td class="text-center" >
         <button type="button" ng-click="view(x)" class="btn btn-success"><i class="fa fa-search-plus"></i></button>
      </td>
   </tr>
  </tbody>
</table>

控制器

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

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

$scope.loadFeedbacks = function () {
            angularFire
                    .getRef("users")
                    .once("value",
                            function (snapshot) {
                                var list = [];
                                snapshot.forEach(function (data) {
                                    var obj = {};
                                    obj = data.val();
                                    obj.id = data.key;
                                    obj.user_id = data.val().user_id;
                                    obj.created_date = new Date(data.val().created_date);
                                    list.push(obj);
                                });
                                $scope.feedbacks = list;
                            },
                            function (error) {
                               console.log(error);
                            }
               );
  };

在你的控制器中试试这个:

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

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

由于下面的答案都不适合我,我只是使用 javascript 排序并关闭 angular 数据表默认排序 withOption('order',[]) 然后它就可以工作了。

工作代码

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

$scope.loadFeedbacks = function () {
            angularFire
                    .getRef("users")
                    .once("value",
                            function (snapshot) {
                                var list = [];
                                snapshot.forEach(function (data) {
                                    var obj = {};
                                    obj = data.val();
                                    obj.id = data.key;
                                    obj.user_id = data.val().user_id;
                                    obj.created_date = new Date(data.val().created_date);
                                    list.push(obj);
                                });

                                // Using Array.sort alternative
                                list = list.sort(function(a, b) {
                                  if (a.created_date > b.created_date) {
                                     return -1;
                                  } else if (a.created_date < b.created_date) {
                                      return 1;
                                  } else {
                                      return 0;
                                   }
                                });
                                $scope.feedbacks = list;
                            },
                            function (error) {
                               console.log(error);
                            }
               );
  };

只需在创建数据表之前更新您的数据,

  • 以这种格式更新日期'YYYY/MM/DD'

    data.forEach(函数(el){ el.Date = moment(el.Date).format('YYYY/MM/) })

  • 并在选项中添加日期列索引:顺序:[[3, 'desc']]

查看官方文档中的日期格式:https://datatables.net/examples/basic_init/table_sorting.html