<div ng:controller="Main"><table>
<tr>
<th><a href ng:click="sortBy('age')">Age</a></th>
<th><a href ng:click="sortBy('date')">Date</a></th>
</tr>
<tr ng:repeat="friend in friends.$orderBy(sort, reverse)">
<td>{{friend.age}}</td>
<td>{{friend.date}}</td>
</tr> </table> </div>
function Main() {
this.friends = [{
age: 10,
date: '11 June 2011'},
{
age: 19,
date: '12 June 2011'},
{
age: 21,
date: '13 July 2011'},
{
age: 35,
date: '14 May 2011'},
{
age: 29,
date: '15 June 2011'}];
}
Main.prototype = {
sort: function(item) {
if (this.predicate == 'date') {
return new Date(item.date);
}
return item[this.predicate];
},
sortBy: function(field) {
if (this.predicate != field) {
this.predicate = field;
this.reverse = false;
} else {
this.reverse = !this.reverse;
}
},
reverse: false
};
請參閱此琴FIDDLE
來源:Sortable table columns with AngularJs
您也可以參考this fiddle too.
這將有助於提供[MCVE] – user2314737