2017-09-28 55 views
0

如何在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); 
          } 
       ); 
    }; 

回答

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); 
          } 
       ); 
    }; 
0

在你的控制器试试这个:

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

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