1

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.

1 Answer 1

1

This: ng-click="sort('{{metadata.columnProperty}}')" should be ng-click="sort(metadata.columnProperty)".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.