Enes Giray
Enes Giray

Reputation: 326

how can i make this filter with ng-repeat

i coding a social media aplication. I have a problem with profile menu in profile page. I want it to be visible menu items depending on certain circumstancesces. I did put menu items in a array like this;

$scope.menuitems = [
    {id : "1", name : "Message", show : "other", url : ""},
    {id : "2", name : "Follow", show : "other", url : ""},
    {id : "3", name : "Followers", show : "all", url : ""},
    {id : "4", name : "About", show : "all", url : ""},
    {id : "5", name : "Statistics", show : "all", url : ""},
    {id : "6", name : "Edit", show : "own", url:""}
];

İf the visible profile is the user's own profile, i want to print items with 'show' value 'own'. If the visible profile is a another user's profile, i want to print items with 'show' value 'other'. And i want to print items with 'show' value 'all' in every stuation. I did a little research on the internet for this but I guess I have not found the right words. How i make this with ng-repeat on Angularjs?

Upvotes: 2

Views: 56

Answers (1)

1Mayur
1Mayur

Reputation: 3485

Create a custom filter

    $scope.conditionVar = 'own'; //this will change depending on what profile

    angular.module('myFilters', []).
  filter('profilefilter', function() {
    return function(items, condition) {
      var out = [];
      for (var i in items) {
        var item = items[i];
        if (item.show === 'all' || item.show === condition) {
            out.push(item);
        }
      }
      return out;
    }
  });
    <li ng-repeat="menuitem in menuitems | profilefilter:conditionVar">{{menuitem}}</li>

here is the working demo - https://jsfiddle.net/0o7ewpgd/1/

Upvotes: 2

Related Questions