テーブル ヘッダーのアイコン クラスの変更に対処するディレクティブを作成しようとしています。私が望むのは(とにかく私が信じていることです)テーブルヘッダーによるソートを処理する標準的な方法です。ディレクティブはリンク要素を追加し、ユーザーがクリックすると降順で並べ替え、アイコンを降順に変更し、もう一度クリックすると昇順で並べ替え、もう一度アイコンを変更します。ここに私がこれまでに持っているものがありますが、アイコンクラスを処理する方法と、同じテーブルにあるがディレクティブのスコープ外にある他の要素をリセットする方法に途方に暮れています。どんな助けでも素晴らしいでしょう!
angular.directive("tableHeaders", function() {
return {
restrict: 'E',
scope: {},
template:'<i class="glyphicon glyphicon-filter"></i>',
link: function(scope, element, attrs) {
attrs.class = 'glyphicon glyphicon-sort-by-alphabet-alt';
}
}
});
これが私がhtml側に持っているものです:
<th>First Name<a ng-click="newOrderBy('_firstName')"><table-headers></table-headers></a></th>
<th>Last Name<a ng-click="newOrderBy('_lastName')"><table-headers></table-headers></a></th>
<tr ng-repeat="item in items | orderBy:orderBy:reverse>
<td>{{item._firstName}}</td>
<td>{{item._lastName}}</td>
</tr>
order by は現在コントローラーで処理されます。
$scope.newOrderBy = function(order) {
$scope.orderBy = order;
$scope.reverse = !$scope.reverse;
};