2017-09-28 3 views
0

Comment commander Date par ordre décroissant dans les données angulaires? J'essaie d'explorer tous question connexe et trouvé celui-ciDonnées angulaires trier par Date décroissante ne fonctionne pas

mais ne fonctionne pas pour moi. L'utilisation de .withOption('order', [[4, 'desc']]) ne fonctionne pas. Alors comment?

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

Contrôleur

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

Répondre

0

Depuis non des réponses ci-dessous travaillé pour moi, je suis juste en utilisant javascript tri et de désactiver données par défaut angulaire tri par défaut avec withOption('order',[]) et puis ça marche.

CODE DE TRAVAIL

$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

Essayez ceci dans votre contrôleur:

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

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