angularjssortingsmart-table

st-sort doesn't work on date column


I am using smart table for my angular project. I am facing an issue for sorting a table column which has Date type data. I get the column info in milliseconds which i convert to Date type. It works as it is when date doesn't have any formatting applied. However, if i apply formatting to make it more easily readable, the sorting doesn't work properly, half the data is sorted and other half is not. How can I sort Date type column if i want to apply formatting as well?

Here's the code snippet of what I have tried so far

for(var i=0; i<$scope.rowCollection.length; i++)
{

$scope.rowCollection[i].ltime = 
(new Date($scope.rowCollection[i].lastRegisteredTime )).toLocaleString();

$scope.rowCollection[i].rtime = 
(new Date($scope.rowCollection[i].registeredTime)).toLocaleString();

}

In this, lastRegisteredTime and registeredTime are data in milliseconds. If I just convert the data to Date without toLocaleString(), sorting works. But, it is little difficult to read the information for users as I want to show the time information as well.

Any help is appreciated. Thanks


Solution

  • Since toLocaleDateString returns a string, you cannot sort by ltime as if it were a real date object. I suggest you leave the field as date object and then use date filter in views, where ever you wish to display the date.

             <span>{{row.ltime|date:'YYYY/MM/DD'}}</span>
    

    or

              <span>{{row.ltime| date:'fullDate'}}</span>