Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to filter angular.js by containment in another array?

So if I have an array:

$scope.letters = 
[{"id":"a"},
{"id":"b"},
{"id":"c"}];

And another array

$scope.filterBy = ["b","c","d"];

And I want to have some ng-repeat to filter $scope.letters by only items that appear in $filterBy.

I want to be able to do something to the effect of:

<span ng-repeat="{{letter in letters|filter: letter.id in filterBy }} > {{letter.id}} </span>

And have it print b,c

I know this is a really stupid example, but is there a way to filter an angular.js expression based on the contents of another array object?

like image 986
infomofo Avatar asked Mar 16 '13 21:03

infomofo


People also ask

Which one is correct way to apply filters in AngularJS?

In AngularJS, you can also inject the $filter service within the controller and can use it with the following syntax for filter. Syntax: $filter("filter")(array, expression, compare, propertyKey) function myCtrl($scope, $filter) { $scope. finalResult = $filter("filter")( $scope.

Which filter is used to select a subset of an array in AngularJS?

The filter filter selects a subset of an array. The filter filter can only be used on arrays, and it returns an array containing only the matching items.

Is the correct way to apply multiple filters in AngularJS?

Answer: A is the correct option. The syntax of applying multiple filters in AngularJS can be written as: {{ expression | filter1 | filter2 | ... }}

How are filters used in AngularJS?

Filters can be added in AngularJS to format data to display on UI without changing the original format. Filters can be added to an expression or directives using the pipe | symbol. AngularJS Filters: AngularJS provides filters to transform data of different data types.


2 Answers

Update

Here's an angular module (based on @InviS answer) to easily implement this filter inside your angular application: filters-inArrayFilter


Here's the angular filters approach based on @InviS answer:

The filter should be like this:

.filter('inArray', function($filter){     return function(list, arrayFilter, element){         if(arrayFilter){             return $filter("filter")(list, function(listItem){                 return arrayFilter.indexOf(listItem[element]) != -1;             });         }     }; }); 

where list is the list you're filtering (this param is set by default by angular), arrayFilter is the array you're using as filter, and element is the name of the property to filter in your list.

To use this filter you use your ng-repeat as:

<div ng-repeat='letter in letters | inArray:filterBy:"id"'>{{letter.id}}</div> 

where inArray is the filter, filterBy (the first argument of this filter) is your array to match against, and "id" (second argument) is the element of the list you want to match against the array.

You can try this live example using the angular filters approach.

like image 183
Cyberdelphos Avatar answered Sep 21 '22 13:09

Cyberdelphos


You should try something like that:

JS:

angular.module('Test', []);

function Ctrl($scope) {
  $scope.letters = [
    {id: 'a'},
    {id: 'b'},
    {id: 'c'}
  ];

  $scope.filterBy = ['b', 'c', 'd'];

  $scope.filteredLetters = function () {
    return $scope.letters.filter(function (letter) {
      return $scope.filterBy.indexOf(letter.id) !== -1;
    });
  };
}

Ctrl.$inject = ['$scope'];

HTML:

<div ng-repeat='letter in filteredLetters(letters)'>{{letter.id}}</div>

You can try live example.

like image 30
ValeriiVasin Avatar answered Sep 23 '22 13:09

ValeriiVasin