user3121518 user3121518 - 1 month ago 25
AngularJS Question

AngularJS table orderBy checkbox

I want to sort a table when a checkbox is clicked and be able to filter the table also. The code below has been fixed and works now....

HTML

<table>
<tr ng-repeat="x in myArray | filter : name | orderBy : sortOrder">
<td>{{ x.name }}</td>
<td>{{ x.age }}</td>
</tr>
</table>

<input type="checkbox" ng-model="sortByName" ng-change="setSortOrder()">
<input type="text" ng-model="name">


AngularJS

app.controller("myController", function($scope, $http)
{
$scope.filterString = '';
$scope.sortByName = false;
$scope.sortOrder = '';

$scope.setSortOrder = function()
{
if($scope.sortByName)
{
$scope.sortOrder = 'name';
}
else
{
$scope.sortOrder = '';
}
}

Answer

You are missing telling your checkbox to call the controller method that selects the sort criteria when its checked status changes. You can do this by using the ngChange directive. Try the following:

<input type="checkbox" ng-model="sortByName" ng-change="setSortOrder()">
Comments