0

How to order date by Descending order in Angular Datatables? I try to explore all of related issue and found this one

but not working for me. Using .withOption('order', [[4, 'desc']]) does not work. Then how to?

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

Controller

$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 Answers 3

0

Try this in your controller:

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

$scope.dtOptions = DTOptionsBuilder.newOptions().withOption('order', [[5, 'desc']]);
Sign up to request clarification or add additional context in comments.

Comments

0

Just update your data before creating a dataTable,

  • update date in this format 'YYYY/MM/DD'

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

  • and add you date column index in options: order: [[3, 'desc']]

see the date format in official docs: https://datatables.net/examples/basic_init/table_sorting.html

Comments

-1

Since non of the answers below worked for me, i'm just using javascript sort and turn off angular datatables default sorting with withOption('order',[]) and then it's working.

WORKING CODE

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.