Angular数据表按Descending Date排序不起作用

时间:2017-09-28 01:49:47

标签: angularjs angular-datatables

如何通过Angular Datatables中的降序排序日期?我尝试探索所有相关问题并找到了这个

但不适合我。使用.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);
                            }
               );
  };

3 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

由于下面的答案中没有一个对我有用,我只是使用javascript排序并关闭角度数据表默认排序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);
                            }
               );
  };

答案 2 :(得分:0)

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

  • 以“ YYYY / MM / DD”格式更新日期

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

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

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