Getting AngularJS orderBy to sort both directions -
i'm attempting setup clickable table column header sort ascending when first clicked, , descending when clicked again. ascending sort working fine, i'm not sure how setup expression within orderby sort descending
my setup far:
table html has like
<th ng-click="sort('lastname')">last name</th>
my sort method looks like
scope.sort = function (columnname) { if (angular.isdefined(scope.filter)) { if (scope.filter.sortcolumn == columnname) { scope.filter.sortcolumn = columnname; scope.filter.sortdirection = scope.filters.sortdirection == "asc" ? "desc" : "asc"; } else { scope.filter.sortcolumn = columnname; scope.filter.sortdirection = "asc"; } } };
and ng-repeat looks follows
<tbody ng-repeat="name in resp.names | orderby : filter.sortcolumn">
how can sortdirection factor orderby?
to reverse you'd change this:
<tbody ng-repeat="name in resp.names | orderby : filter.sortcolumn : true">
it'd best if used boolean in controller, should work too:
<tbody ng-repeat="name in resp.names | orderby : filter.sortcolumn : filter.sortdirection === 'desc'">
and fun, here's how sorting filtering in tables.
controller:
$scope.search = { query: ''}; $scope.sort = { field: 'defaultfield', descending: true}; $scope.order = function(newvalue) { if(newvalue === $scope.sort.field) { $scope.sort.descending = !$scope.sort.descending; } else { $scope.sort = {field: newvalue, descending: false}; } }; $scope.filtereddocuments = function() { var = $filter('filter')($scope.documents, {$:$scope.search.query}); var b = $filter('orderby')(a, $scope.sort.field, $scope.sort.descending); return b; };
a search box filtering:
<input type="text" ng-model="search.query">
a column header:
<th nowrap> <a href ng-click="order('size')">size </a> <i ng-show="sort.field === 'size' && !sort.descending" class="fa fa-sort-amount-asc"></i> <i ng-show="sort.field === 'size' && sort.descending" class="fa fa-sort-amount-desc"></i> </th>
the row binding:
<tr ng-repeat="d in filtereddocuments()" >
Comments
Post a Comment