3

I have a simple method of sorting the table column implemented but I can't figure out a way to alternate between reverse the sorting on click and back. Does anyone have any solutions to this issue? Below is a fiddle showing you what I mean.

<div ng-app="app">

<div ng-controller="controller">

    <p>{{orderProperty}}</p>
    <div class="col-md-10">
        <table class="table table-hover table-bordered">
            <thead>
            <tr>
                <th>Status<a ng-click="orderProperty = 'a'">^</a></th>
                <th>Ref<a ng-click="orderProperty = 'b'">^</a></th>
                <th>Service<a ng-click="orderProperty = 'c'">^</a></th>
                <th>Domain<a ng-click="orderProperty = 'd'">^</a></th>
                <th>Service Owner<a ng-click="orderProperty     = 'e'">^</a></th>
                <th>Stage<a ng-click="orderProperty = 'f'">^</a></th>
            </tr>
            </thead>
            <tbody>
                <tr ng-repeat="x in projects | orderBy:orderProperty">
                    <td><b>{{x.a}}</b></td>
                    <td>{{x.b}}</td>
                    <td>{{x.c}}</td>
                    <td>{{x.d}}</td>
                        <td>{{x.e}}</td>
                        <td>{{x.f}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

http://jsfiddle.net/ben1729/3nxykbhk/

3 Answers 3

2

You could toggle the orderProperty by prefixing the column name with '-'. Replace your table header with this code:

<thead>
    <tr>
        <th>Status<a ng-click="setOrderProperty('a')">^</a></th>
        <th>Ref<a ng-click="setOrderProperty('b')">^</a></th>
        <th>Service<a ng-click="setOrderProperty('c')">^</a></th>
        <th>Domain<a ng-click="setOrderProperty('d')">^</a></th>
        <th>Service Owner<a ng-click="setOrderProperty('e')">^</a></th>
        <th>Stage<a ng-click="setOrderProperty('f')">^</a></th>
    </tr>
</thead>

... and add this function to your scope:

$scope.setOrderProperty = function(propertyName) {
    if ($scope.orderProperty === propertyName) {
        $scope.orderProperty = '-' + propertyName;
    } else if ($scope.orderProperty === '-' + propertyName) {
        $scope.orderProperty = propertyName;
    } else {
        $scope.orderProperty = propertyName;
    }
}

http://jsfiddle.net/3nxykbhk/1/

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

1 Comment

How I can sort data in decimal format. like 10.32,34.43.22.78
1

You can reverse the ordering by passing true as the 3rd part of the orderBy filter:

<tr ng-repeat="x in projects | orderBy : orderProperty : true">

1 Comment

Ah, should have been more clear. What I mean to ask was how to alternate between reversed and normal sorting on click.
1

In the html

change the ng-click to call a function

<tr>
    <th><a ng-click="sortProperty('a')">Status</a></th>
    <th><a ng-click="sortProperty('b')">Ref</a></th>
    <th><a ng-click="sortProperty('c')">Service</a></th>
    <th><a ng-click="sortProperty('d')">Domain</a></th>
    <th><a ng-click="sortProperty('e')">Service Owner</a></th>
    <th><a ng-click="sortProperty('f')">Stage</a></th>
</tr>

In the Javascript:

add a '+' to the orderProperty variable

$scope.orderProperty = "+f";

then add this function

$scope.sortProperty = function(column) {
    var currentColumn = $scope.orderProperty.slice(1);
    var currentDirection = $scope.orderProperty.slice(0, 1);
    var dir = '+';

    if (column === currentColumn) {
        dir = currentDirection === '+' ? '-' : '+';
    }
    $scope.orderProperty = dir + column;
};

Clicking on the column header will now toggle the sort

see JsFiddle demo

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.