Reputation: 5355
I want to sort columns in ng-repeat in AngularJs.
I have something like this:
<th class="cl_sort_init" ng-click="predicate='EstimatedCloseDate';
reverse=!reverse" ng-class="{'cl_sort_desc':
predicate=='EstimatedCloseDate'&&reverse==true, 'cl_sort_asc':
predicate=='EstimatedCloseDate'&&reverse==false}">Close Date</th>
However EstimatedCloseDate is a string. Hence it doesnt work the way it should. How do I make it work as dates just for this column. Other columns are strings and they work just fine.
Any ideas and suggestions !!!
Upvotes: 1
Views: 103
Reputation: 81489
In your predicate you should be able to convert the string to date time. No other conversion should be needed. See here for string to date conversion: Converting string to date in js and format particulars.
If your date format is unusual check out libraries like datejs for help.
But here are a few snippets converting several date strings into dates. Notice that the last conversion fails.
var dtString = "2010-12-25";
var dt = new Date(dtString);
console.log(dtString + " = " + dt);
dtString = "12/25/2010";
dt = new Date(dtString);
console.log(dtString + " = " + dt);
dtString = "25/12/2010";
dt = new Date(dtString);
console.log(dtString + " = " + dt);
Output:
2010-12-25 = Fri Dec 24 2010 18:00:00 GMT-0600 (CST) 12/25/2010 = Sat Dec 25 2010 00:00:00 GMT-0600 (CST) 25/12/2010 = Invalid Date
Upvotes: 0
Reputation: 2057
Depending on the format of the date string, you might be able to do
(new Date(EstimatedCloseDate))
This will parse the date string then convert it into a date object, which will play nice with sorting If this doesn't work, you will have to write your own date parser, which eventually turns the date into an int or Date object
Upvotes: 1