-1

Hi everyone how can i filter the values in angularjs?

  • I have created plunker for reference :- My Plunker.

  • I want to filter user categories in ng-repeat question list page

User datas:-

"user": {
"_id": "58072aba0f82a61823c434df",
"displayName": "Table 1",
"dob": "2016-12-22T18:30:00.000Z",
"location": "chennai",
"religion": "hindu",
"roles": [
"admin"
],
"profileImageURL": "./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136"
},
  • categories values are in array, i need to filter categories values only in ng-repeat list page....

  • For Example:- if user categories values "categories": [ "Religion & Culture", "Social Psychology" ], so these two values only should filter in list of category...

My Html:-

    <div ng-repeat="question in questions | filter:user.categories ">
    <small>
                    <span >{{$index + 1}}.</span>
                      <span data-ng-bind="question.category"></span>
                  </small>

  </div>
  • I have used filter in ng-repeat like :- | filter:user.categories

  • I want to filter user categories in repeat list...

  • please check and update the plunker as well to know the exact solution...

R. Mani Selvam
  • 320
  • 8
  • 36
  • what do you want to filter your list with? you need to have some data regarding `categories` I guess? – tanmay Apr 07 '17 at 09:04
  • is this just a homework? what have you tried before post here? – Pengyy Apr 07 '17 at 09:38
  • @tanmay please look at my plunker i have user data like categories and i need to filter these categories in ng-repeat list, please check my plunker and update the same...thanks..... – R. Mani Selvam Apr 07 '17 at 10:35

2 Answers2

1

You need to create custom filter like:

.filter('filterCategory', [
 function() {
  return function(questions) {
   var filteredCategories = [];
   if (questions && questions.length) {
    angular.forEach(questions, function(question) {
      console.log(question)
      if(question.user && question.user.categories && question.user.categories.length){
        angular.forEach(question.user.categories, function(category){
          if(filteredCategories.indexOf(category)<0){
            filteredCategories.push(category);
          }
        })
      }
    })
  }
   return filteredCategories;
 }
}

])

And then in the ng-repeat you need to write:

<div ng-repeat="category in questions| filterCategory ">
Indranil Mondal
  • 2,799
  • 3
  • 25
  • 40
  • Yeah you can check this plunk : http://plnkr.co/edit/wM65xfwxvdGHczAmTBHg?p=preview – Indranil Mondal Apr 08 '17 at 13:23
  • Thanks for your answer , please look at my plunker there are three `Religion & Culture` is available but yours plunker showing only one `Religion & Culture`...we need to filter all `Religion & Culture` and need to show number of `Religion & Culture` also...please check and help...thanks – R. Mani Selvam Apr 10 '17 at 08:37
  • Sorry you question in not quite clear to me, for each data there is a user object and then for each user there is a categories array. So you want to show all those categories in the list? I thought you want to show unique values. – Indranil Mondal Apr 11 '17 at 09:30
  • yes exactly for each data there is a user object and then for each user there is a categories array, if authenticate user is signed into portal , authenticated user categories array should filter in that list...for example:- if `"displayName": "Table 1",` this user signed into portal this particular user categories only need to filter....please check my plunker...thanks for your help – R. Mani Selvam Apr 12 '17 at 10:07
  • Ok, then you can check my updated plunk : http://plnkr.co/edit/wM65xfwxvdGHczAmTBHg?p=preview – Indranil Mondal Apr 12 '17 at 10:46
  • thank you Indra you almost done...please once again look at my plunker http://plnkr.co/edit/Sz4ZkvdtvIonw5jdQ6BE?p=preview in the list there are various category is displaying, but the user has `Religion & Culture and Social Psychology` categories,so what i am expecting just want to filter user categories in that `list` category, for example:- there four category is displaying in list, so filter should work based on the user categories, thanks indra... i have tried to filter like `| filter:user.categories "` in ng-repeat but it's not working for me so please check and help thanks.... – R. Mani Selvam Apr 13 '17 at 06:18
  • Sorry your requirement is still not clear to me. You have a category for each question and then in each question there is a user and in the user there is a categories array. So you want to show the category in each question if that category is present in the categories array of the user for the same question? – Indranil Mondal Apr 13 '17 at 20:15
0

Instead of filter: user.categories try filter: question.user.categories

user7761868
  • 94
  • 1
  • 6