I am trying to make sortable table. I wrote the following View and Controller:
View:
<div class="container" ng-app>
<div ng-controller="SortableTblCtrl">
<table class="table table-hover table-bordered span12">
<tr>
<th ng-repeat="metadata in metadatas" ng-click="sort('{{metadata.columnProperty}}')" style="cursor: pointer">{{metadata.columnName}} </th>
</tr>
<tr ng-repeat="person in persons | orderBy:predicate:reverse">
<td>{{person.firstName}}</td>
<td>{{person.lastName}}</td>
</tr>
</table>
</div>
Controller:
function SortableTblCtrl($scope) {
$scope.persons = [
{ "firstName": "firstname1", "lastName": "lastname1" },
{ "firstName": "firstname2", "lastName": "lastname2" },
{ "firstName": "firstname3", "lastName": "lastname3" }
];
$scope.predicate = "";
$scope.reverse = false;
$scope.sort = function (pred) {
$scope.predicate = pred;
$scope.reverse = !$scope.reverse;
};
$scope.metadatas = [
{ "columnProperty": "firstName", "columnName": "First Name" },
{ "columnProperty": "lastName", "columnName": "Last Name" }
];
}
When I click on column headers nothing happens. I found something, that is when I change ng-click="sort('{{metadata.columnProperty}}')" to ng-click="sort('firstName')" it sorts first column when I click on every column header.
This: ng-click="sort('{{metadata.columnProperty}}')" should be ng-click="sort(metadata.columnProperty)".