1

I have a list of data, and grouped that data depends on category. In a drop down that shows that category name. When I select a particular category, then it will shows only that category list. If I select "All Category" then it will show all category list.
Please see the below link and code.

http://plnkr.co/edit/gNKQpUT2OwtFkxxyTwPY

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

app.controller('MainCtrl', function($scope) {
$scope.locationList = [
{
"tid": "2440",
"name": "Alfredo's Pizzeria",
"field_location_category": [
  "Food and Dining"
]
},
{
"tid": "2441",
"name": "Allegro Dining Room",
"field_location_category": [
  "Food and Dining"
]
},
{
"tid": "2443",
"name": "Art Gallery",
"field_location_category": [
  "Gifts & Memories"
]
},
{
"tid": "2435",
"name": "Bellini's",
"field_location_category": [
  "Entertainment/Bars"
]
},
{
"tid": "2498",
"name": "Bullseye",
"field_location_category": [
  "Pools, Sports & Spa"
]
}
];
var indexedloc = [];
        $scope.locationListToFilter = function(){
            indexedloc = [];
            return $scope.locationList; 
        }

        $scope.filterLocation = function(Loc){
            var locationIsNew = indexedloc.indexOf(Loc.field_location_category[0]) == -1;
            if(locationIsNew){
                indexedloc.push(Loc.field_location_category[0]);                    
            }
            return locationIsNew;
        }   
        $scope.returnFilterLoc = function(){return indexedloc};
    });

HTML

 <body ng-controller="MainCtrl">
<select id="category_select" ng-options="loc as loc for loc in returnFilterLoc()" ng-model="locationList">
    <option value="">All Categories</option>
</select>
<nav id="entertainment_bars" class="persist-area content-list l2g-menu-list" ng-repeat="loc in locationListToFilter() | filter:filterLocation">
  <h3 class="persist-header">{{loc.field_location_category[0]}}</h3>
  <ul>
    <li ng-repeat="Loc in locationList | filter:{field_location_category:loc.field_location_category[0]}">
    <span>{{Loc.name}}</span> </li>
  </ul>
</nav>

Please help me. Thanks in advance.

1 Answer 1

2

I have forked your plunker

Controller

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

app.controller('MainCtrl', function($scope) {
  $scope.locationList = [
  {
    "tid": "2440",
    "name": "Alfredo's Pizzeria",
    "field_location_category": [
      "Food and Dining"
    ]
  },
  {
    "tid": "2441",
    "name": "Allegro Dining Room",
    "field_location_category": [
      "Food and Dining"
    ]
  },
  {
    "tid": "2443",
    "name": "Art Gallery",
    "field_location_category": [
      "Gifts & Memories"
    ]
  },
  {
    "tid": "2435",
    "name": "Bellini's",
    "field_location_category": [
      "Entertainment/Bars"
    ]
  },
  {
    "tid": "2498",
    "name": "Bullseye",
    "field_location_category": [
      "Pools, Sports & Spa"
    ]
  }
];
  var indexedloc = [];
  $scope.categories = [];
  angular.forEach($scope.locationList, function(item){
    if($scope.categories.indexOf(item.field_location_category[0]) === -1){
      $scope.categories.push(item.field_location_category[0]); 
    }
  });

    $scope.locationListToFilter = angular.copy($scope.locationList);

    $scope.filterLocation = function(Loc){
      $scope.locationListToFilter.length = 0;
      if($scope.selectedCategory){
        angular.forEach($scope.locationList,function(item){
        if(item.field_location_category[0] === $scope.selectedCategory){
            $scope.locationListToFilter.push(item);
        }
        });
      }else{
        Array.prototype.push.apply($scope.locationListToFilter, $scope.locationList);
      }
    }   
});

HTML

<body ng-controller="MainCtrl">
    <select id="category_select" ng-options="loc as loc for loc in categories" ng-model="selectedCategory"
    ng-change="filterLocation()">
        <option value="">All Categories</option>
    </select>
    <nav id="entertainment_bars" class="persist-area content-list l2g-menu-list" ng-repeat="loc in locationListToFilter">
      <h3 class="persist-header">{{loc.field_location_category[0]}}</h3>
      <ul>
        <li ng-repeat="Loc in locationList | filter:{field_location_category:loc.field_location_category[0]}">
        <span>{{Loc.name}}</span> </li>
      </ul>
    </nav>
  </body>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much Shripal Soni. I have one concern, The first category is listing twice.
Please check the updated plunker

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.