0

I have created an application using ng-table, The application is working fine, sorting is working fine except the date column, can anyone please tell me some solution for this. My code is as given below

Working Demo

html

<table ng-table="tableParams" class="table">
    <tr ng-repeat="user in myValues">
        <td data-title="'Name'" sortable="'name'">
            {{user.name}}
        </td>
        <td data-title="'Age'" sortable="'age'">
            {{user.age}}
        </td>
        <td data-title="'Last Update Date'" sortable="'lastUpdateDate'">
            {{user.lastUpdateDate}}
        </td>
    </tr>
</table>

script

var app = angular.module('main', ['ngTable']).
controller('DemoCtrl', function($scope, $filter, ngTableParams) {
    $scope.myValues = [{name: "Moroni", age: 50, lastUpdateDate:"03/14/2014 06:21"},
                {name: "Tiancum", age: 43, lastUpdateDate:"07/24/2014 06:41"},
                {name: "Jacob", age: 27, lastUpdateDate:"09/10/2001 04:46"},
                {name: "Nephi", age: 29, lastUpdateDate:"03/30/2013 03:31"},
                {name: "Enos", age: 34, lastUpdateDate:"02/20/2004 09:11"},
                {name: "Tiancum", age: 43, lastUpdateDate:"07/12/2012 05:24"},
                {name: "Jacob", age: 27, lastUpdateDate:"01/24/2013 01:11"},
                {name: "Nephi", age: 29, lastUpdateDate:"05/02/2014 02:21"},
                {name: "Enos", age: 34, lastUpdateDate:"01/21/2001 03:31"},
                {name: "Tiancum", age: 43, lastUpdateDate:"02/14/2002 04:21"},
                {name: "Jacob", age: 27, lastUpdateDate:"03/22/2022 05:01"},
                {name: "Nephi", age: 29, lastUpdateDate:"04/14/2020 06:11"},
                {name: "Enos", age: 34, lastUpdateDate:"05/03/2002 07:03"}, 
                {name: "Tiancum", age: 43, lastUpdateDate:"06/09/2000 08:01"},
                {name: "Jacob", age: 27, lastUpdateDate:"07/10/2001 09:02"},
                {name: "Nephi", age: 29, lastUpdateDate:"08/27/2012 10:10"},
                {name: "Enos", age: 34, lastUpdateDate:"09/12/2014 06:12"}];

    $scope.tableParams = new ngTableParams({
        sorting: {
            lastUpdateDate: 'desc'     
        }
    }, {
        getData: function($defer, params) {
           $scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy());
          $defer.resolve($scope.myValues);
        }
    });
});
Alex Man
  • 4,746
  • 17
  • 93
  • 178
  • @Pablo this is related with ng-table – Alex Man Oct 07 '14 at 14:56
  • No, he's right - that's the entirety of the problem. [new plunkr](http://plnkr.co/edit/g7nQEyui8DndzYBXbsKp?p=preview) -- store the strings as Date objects and format them for display [using a filter](https://docs.angularjs.org/api/ng/filter/date). – Blazemonger Oct 07 '14 at 14:58
  • @AleMan I've just added a filter to the edit of Blazemonger, check what we [mean here](http://plnkr.co/edit/sqO7FFuCpdTlOwlLfanv?p=preview) – Pablo Lozano Oct 07 '14 at 15:14
  • But how can we change it to date object....since the json with the date as string is comming from an external application and is not static like as shown in the plunker – Alex Man Oct 07 '14 at 16:12

1 Answers1

1

It works, but obviously not in the way you are expecting: There is nothing in your code saying that field is a Date, so the lastUpdateDate is being handled as a common String, so the order is alphabetical.

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59