How can I sort data by date in smart-table? With st-sort it isn't so good.
<table st-table="displayedCollection" st-safe-src="playerCollection" class="table table-hover">
<thead>
<tr>
<th st-sort="id" class="hover">Id</th>
<th st-sort="firstname" class="hover">Jméno</th>
<th st-sort="lastname" class="hover">Příjmení</th>
<th st-sort="registrationDate" class="hover">Datum registrace</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="player in displayedCollection">
<td class="hover">{{player.id}}</td>
<td class="hover">{{player.firstname}}</td>
<td class="hover">{{player.lastname}}</td>
<td class="hover">{{player.registrationDate}}</td>
</tr>
</tbody>
</table>
Thanks for answers.
It should normally work (cf documentation website). However if your registration date is a date string you should use a getter to return the date object version of it, otherwise you'll have the alphaNumeric order
in your controller
$scope.getters = {
registrationDate:function(row) {
return new Date(row.registrationDate);
}
}
so you can bind your header to this getter
<th st-sort="getters.registrationDate">Datum registrace</th>
Add an order by to your ng-repeat
like this:
<tr ng-repeat="player in displayedCollection | orderBy:'registrationDate'">
<td class="hover">{{player.id}}</td>
<td class="hover">{{player.firstname}}</td>
<td class="hover">{{player.lastname}}</td>
<td class="hover">{{player.registrationDate}}</td>
</tr>
For sorting onclick
, you could add a variable to the scope that determines the sorting and used that on the orderBy
on the ng-repeat
.
Something like this:
<table st-table="displayedCollection" st-safe-src="playerCollection" class="table table-hover">
<thead>
<tr>
<th st-sort="id" class="hover"><a href="" ng-click="predicate = 'id'; reverse=false">Id</a></th>
<th st-sort="firstname" class="hover"><a href="" ng-click="predicate = 'firstname'; reverse=false">Jméno</a></th>
<th st-sort="lastname" class="hover"><a href="" ng-click="predicate = 'lastname'; reverse=false">Příjmení</a></th>
<th st-sort="registrationDate" class="hover"><a href="" ng-click="predicate = 'registrationDate'; reverse=false">Datum registrace</a></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="player in displayedCollection | orderBy:predicate:reverse">
<td class="hover">{{player.id}}</td>
<td class="hover">{{player.firstname}}</td>
<td class="hover">{{player.lastname}}</td>
<td class="hover">{{player.registrationDate}}</td>
</tr>
</tbody>
</table>
I created a plunker for this. You'll see if you click on a column header it'll sort the table by that column. I hope it helps.
http://plnkr.co/edit/pAJ3PpRwVk7PuTmoMjsr?p=preview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With