1

I am new in AngularJS...I created a filter on an output data. Next I need to sort those array data based on a dropdown selected item. I am not getting it.The filter processing is done in the 3rd directive. Any help is appreciated.

 $scope.options=[
              {label: 'Name', value:1,submenu:null},
              {label: 'Age', value:2,submenu:null},
              {label: 'Country', value:3,submenu:[
                      {label:'India',value:1},
                      {label:'China',value:2},
                      {label:'Singapore',value:3},
                      {label:'London',value:4}
                  ]}
          ];

 <select ng-model="model1" ng-options="opt as opt.label for opt in options">
        </select>

here is the plunker http://plnkr.co/edit/BFEJQo8Zp2eukdgP9BKM?p=preview

5
  • I didnt understand what you expecting ? Commented Jul 24, 2014 at 8:49
  • I created a form first and submitted the data . I retrieved the data and displayed on the form and created a filter based on gender.The filter is also working and getting the filtered data. Next I need to sort this filtered data based on a selected item from a drop down list.this list contains Name,age and country which in turn is a list of 4 country names. Commented Jul 24, 2014 at 8:57
  • I used three directives and a service. three html files for creating,displaying form and displaying filtered data. Commented Jul 24, 2014 at 8:59
  • what you mean by asking "Next I need to sort this filtered data based on a selected item from a drop down list" Commented Jul 24, 2014 at 9:17
  • Next I m trying to do that sorting...thats wht i meant. Commented Jul 24, 2014 at 9:19

1 Answer 1

3

Try using a custom ordering in ng-repeat.

Working Demo

<div ng-repeat="detail in details | orderBy : sortingOrder : reverse" ng-show="showResults">
    {{detail.Fname | uppercase}}
    {{detail.Lname}}
    {{detail.age}}
    {{detail.sex}}
    {{detail.place}}
</div>

script

  $scope.search = function() {
    $scope.reverse = '';
    $scope.showResults = true;
    if ($scope.model1.label === 'Age') {
      $scope.sortingOrder = 'age';
    } else if ($scope.model1.label === 'Name') {
      $scope.sortingOrder = 'Fname';
    } else if ($scope.model1.label === 'Country') {
      $scope.sortingOrder = 'place';
    }
     else if ($scope.model1.label === 'Gender') {
       console.log($scope.user.sex1);

     if($scope.user.sex1 === 'male')
     {
       $scope.reverse = true;
     }
     else if($scope.user.sex1 === 'female')
     {
      $scope.reverse = false;
     }
      $scope.sortingOrder = 'sex';
    }
  };
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.