0

I want to filter angularjs for the following json format value

[

{"id":"1","video_title":"Sample1","tags":"2,12,13","featured":"1"},

{"id":"2","video_title":"Sample2","tags":"5,13","featured":"1"},

{"id":"2","video_title":"Sample3","tags":"2,13","featured":"0"}

] 



<ul><li ng-repeat="single in all_json | filter:customFilter"></li></ul>

<a ng-click="filterBasedonTag(2)">Filter</a>

// Script

 $scope.filterBasedonTag = function(x) {
 $scope.customFilter = x;
}

If I pass tags value = 2 I want to print only video_title "sample1" and "Sample 3". It is possible to filter. Is any solution please. Thanks to all

3
  • Possible duplicate of AngularJS custom filter function Commented Apr 23, 2018 at 13:35
  • Bro I need to compare inside tags element, your given duplicate thing is different Commented Apr 23, 2018 at 13:38
  • You can still use the link he gave you. You just need to adapt it to your use case. Commented Apr 23, 2018 at 13:57

2 Answers 2

1

You can pass parameters into a filter with ... | your_filter : {"anything" : parameters} syntax, where parameters are bounded to scope and can be changed dynamically. Here is an example with a simple custom filter:

var app = angular.module('myApp', []);
app.filter('customFilter', function() {
  return function(x, y) {
    var temp = [];
    for (var i = 0; i < x.length; i++) {
      if (x[i].tags.split(',').indexOf("" + y.tag) != -1) {
        temp.push(x[i]);
      }
    }
    return temp;
  };
});
app.controller('namesCtrl', function($scope) {
  $scope.all_json = [{
      "id": "1",
      "video_title": "Sample1",
      "tags": "2,12,13",
      "featured": "1"
    },
    {
      "id": "2",
      "video_title": "Sample2",
      "tags": "5,13",
      "featured": "1"
    },
    {
      "id": "2",
      "video_title": "Sample3",
      "tags": "2,13",
      "featured": "0"
    }
  ]
  $scope.tag = 2;
  $scope.filterBasedonTag = function(tag) {
    $scope.tag = tag;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<div ng-app="myApp" ng-controller="namesCtrl">
  <li ng-repeat="single in all_json | customFilter:{'tag':tag}">
    {{single.video_title}} - {{single.tags}}
  </li>
  <hr>
  <a ng-click="filterBasedonTag(2)">Filter 2 </a><br>
  <a ng-click="filterBasedonTag(5)">Filter 5 </a><br>
  <a ng-click="filterBasedonTag(13)">Filter 13 </a><br>
  <hr> 
  Selected tag: {{tag}}
</div>

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

2 Comments

Hi Bro its working. Thanks! I have doubt if i pass two tag value at a time in checkbox how to filter. <input type="checkbox" ng-model="tagsarray.tag" name="tag" ng-click="filterBasedonTag(tagsarray.tag)" /><input type="checkbox" ng-model="tagsarray.tag" name="tag" ng-click="filterBasedonTag(tagsarray.tag)" />
@Rijo checkbox ng-model returns only true or false, but if you need to pull out a tag number, then maybe you would want to use ng-true-value and specify the number there. Here is an example: <input type="checkbox" ng-model="tagsarray.tag" ng-true-value="13" ng-false-value=""/>
1

Using a custom function. This is an adapted version of the duplicate link in comments.

BEWARE: The custom function I wrote is very simple and will return values contains just a part of the criteria, so in this case 2 will return everything containing a 2 (12, 20, 32, 200, ...). I suggest you write a proper function yourself.

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.criteria = 2;
  
  $scope.criteriaMatch = function( criteria ) {
    return function( item ) {
      return item.tags.indexOf(criteria) > -1;
    };
  };
  
  $scope.items =[
{"id":"1","video_title":"Sample1","tags":"2,12,13","featured":"1"},
{"id":"2","video_title":"Sample2","tags":"5,13","featured":"1"},
{"id":"2","video_title":"Sample3","tags":"2,13","featured":"0"}
];
});
<html ng-app="plunker" >
<head>
  <meta charset="utf-8">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body ng-controller="MainCtrl">
  <input type="text" ng-model="criteria">
  <div ng-repeat="item in items | filter:criteriaMatch(criteria)">
    {{ item | json }}
  </div>
</body>
</html>

3 Comments

if you search for 3 it selects objects with tags containing 13, as if 3 is in there. You should attempt to split the tags strings first and search within the array instead
That's for himself to do. I did add a notice to the main post suggesting he writes a proper function himself.
Hi bro instead of <input type="text" ng-model="criteria"> how to hadle with two three checkbox, that means i need to transfer multiple values.

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.