2015-10-29 78 views
0

這是我的表格中的一列。它顯示結束日期。我需要點擊列標題進行排序。我想對列中的日期進行排序

<th class="Theader"> 
<a href="" ng-click="predicate = 'endDate'; reverse=reversesort;reversesort=!reversesort" class="blackfont" style="color: #656565;" title="Click to sort"> 
    End Time 
</a> 
<span class="ordermenu fa fa-sort-asc" ng-show="!reversesort"></span><span class="ordermenu fa fa-sort-desc" ng-show="reversesort"></span> 
</th> 
+0

這將有助於提供[MCVE] – user2314737

回答

3
<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.

+0

從來沒有意識到,你可以替換使用連字符'NG -...'帶冒號':...'。有趣。 – TheLimeTrees

+1

@TheLimeTrees [鏈接](https://github.com/angular/code.angularjs.org/blob/master/0.9.14/docs-0.9.14/angular.directive.ng:autobind.html) –

相關問題