2

I have an array $scope.something = ['xyz','abc','pqr'];

On click, I want to filter the ng-repeat data so that it will return the resulting JSON data filtered with 'xyz', 'pqr', and 'abc'.

ng-repeat="data in flt_det | filter:something".

Thanks in advance.

2
  • This may help Commented Apr 2, 2015 at 16:42
  • no this is some other scenario. the click is on a button. Commented Apr 2, 2015 at 16:50

2 Answers 2

2

if you want to filter data with any of the matching elements inside $scope.something then you may need to craete a custom filter:

app.filter("somethingFilter", function() {
   return function(array, something) {
     return array.filter(function(ele) {
          return (something.indexOf(ele) > 0)
      });
   }
});

Now in your HTML,

ng-repeat="data in flt_det | filter:something".

This will filter flt_det if the data is present in something array.

ON click of a button?

$scope.buttonClickHandler = function() {
     $scope.flt_det = $scope.flt_det.filter(function(ele) {
                               return ($scope.something.indexOf(ele) > 0)
                        });
}

This will update the $scope.flt_det data on click of the button.

You can even use $filter in this case as well:

app.controller("myCtrl", function($scope, $filter)
{
    $scope.buttonClickHandler = function() {
        $scope.flt_det = $filter('somethingFilter')($scope.flt_det,$scope.something);
    };
});

EDIT DEMO

angular.module("app",[])
.controller("MainCtrl", function($scope) {
 $scope.flt_det = ["abc", "pqr", "sdfs", "sdfds", "xyz"];
 $scope.something = ["abc", "pqr", "xyz"];
 $scope.buttonClickHandler = function() {
  $scope.flt_det = $scope.flt_det.filter(function(ele) {
        return ($scope.something.indexOf(ele) > 0)
     });
  }
});
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-controller="MainCtrl">
<div ng-repeat="data in flt_det">
   {{data}}
</div>
<button type="button" ng-click="buttonClickHandler()">Filter data</button>
</body>
</html>

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

3 Comments

i need the result based on the 3 value and can we do this operation on click of a single button ?
Updated with buttonClick handler.
Added a demo as well :) @user2131395
1

EDIT: Fixed to address question of filtering using the array input

Another option if you want to use the built-in Angular filters within your controller instead of a custom filter, is to do something like this (working JSFiddle):

$scope.filtered = [];
    for(var index = 0; index < $scope.something.length; index++) {
      var singleFilter = $filter('filter')($scope.flt_det, $scope.something[index]);
      $scope.filtered = _.union($scope.filtered, singleFilter);
    }

This maintains a separate $scope.filtered array. It also utilizes Underscore.js's union function. Then your html will look like this:

ng-repeat="data in filtered"

See the Angular documentation here for using a filter in the controller: https://docs.angularjs.org/api/ng/filter/filter

2 Comments

The OP wants to use $scope.something array to filter another array $scope.flt_det. But $filter will filter the first argument array with a primitive value which is second argument.
Thanks @mohamedrias, definitely misunderstood the question. Fixed the answer

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.