2

i am trying to do orderBy using dropdown value, but its not working :(

CODE:

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
    $scope.datas = [{
        "id": "1",
            "name": "name area 1"
    }, {
        "id": "2",
            "name": "name area 2"
    }, {
        "id": "3",
            "name": "name area 3"
    }, {
        "id": "4",
            "name": "name area 4"
    }];
    $scope.dropdown = [{
        "title": "Newest first",
            "value": "true"
    }, {
        "title": "Oldest first",
            "value": "reverse"
    }];
});

HTML:

    <div ng-app="myApp">
      <div ng-controller="myCtrl">
          <ul ng-repeat="rows in datas | orderBy: 'id':orderByData">
              <li>{{rows.id}} : {{rows.name}}</li>
          </ul>
          <select ng-model="orderByData" ng-options="x.value as x.title for x in dropdown"></select>
      </div>
    </div>

DEOM JS BIN

0

2 Answers 2

2

Why you use reverse? Please use false instead reverse and it should work fine.

$scope.dropdown = [{
        "title": "Newest first",
            "value": "true"
    }, {
        "title": "Oldest first",
            "value": "false"
    }];
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need reverse parameter here. Instead you can do something like this:

<ul ng-repeat="rows in datas | orderBy: getSort()">
    <li>{{rows.id}} : {{rows.name}}</li>
</ul>

Where getSort is defined as:

$scope.getSort = function() {
    return $scope.orderByData ? 'id' : '-id';
};

Basically you want sort part to look like orderBy: 'id' or orderBy: '-id'.

Demo: http://jsbin.com/pepureta/1/edit?html,js,output

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.