0

I am using angularjs v 1.2.

My HTML code:

<div ng-repeat="activity in currentActivitiesList" >
            <h3 style="color: green;" align="center"><bold>{{activity.seasonName}}</bold></h3><hr />    
            <div class="col-xs-4">
                <img src="images/image1.png" />
            </div>
            <div class="col-xs-8">
                <p>{{activity.activityName}}</p>      
                <p>{{activity.date}}</p>   
                <p style="color: orange;">Status: {{activity.status}}</p>
            </div>
            <div style="clear:both;"></div>
            <hr />  

        </div>

My controller code:

var filterActivities = function(){
  angular.forEach($scope.activitiesList, function(value, key) {
  //console.log("key -->" + key + "and value --> " + value.date);
  var splitDate = value.date.split('-');

  if(splitDate[0] == $scope.currentNavYear){
    if(value.seasonCode == 1){
      value.seasonName = "Early Spring February - March";
    }
    if(value.seasonCode == 2){
      value.seasonName = "Late Spring April -May";
    }
    if(value.seasonCode == 3){
      value.seasonName = "Rest of the year!";
    }
    $scope.currentActivitiesList.push(value);
    };
  });
}

filterActivities();

My JSON data is:

$scope.activitiesList = [
            {
                "activityName":"Activity 1",
                "regionCode":"N",
                "date":"2014-04-15",
                "status": "Scheduled",
                "seasonCode":1
            },
            {
                "activityName":"Activity 2",
                "regionCode":"N",
                "date":"2014-04-15",
                "status": "Completed",
                "seasonCode":1
            },
            {
                "activityName":"Activity 3",
                "regionCode":"N",
                "date":"2014-04-15",
                "status": "Completed",
                "seasonCode":2
            },
            {
                "activityName":"Activity 4",
                "regionCode":"N",
                "date":"2014-04-15",
                "status": "Completed",
                "seasonCode":3
            },
            {
                "activityName":"Activity 3",
                "regionCode":"N",
                "date":"2013-04-15",
                "status": "Scheduled",
                "seasonCode":3
            }
        ];

I want to display my activities season wise like this:

Early Spring February - March

All activities in this season

Late Spring April -May

All activities in this season

How do i achieve it? Do i create a custom filter or add some more logic in my controller?

Also is there any way to iterate ng-repeat and display its key once and the value (array) depending on its length?

spyder
  • 330
  • 3
  • 6
  • 18
  • Using [`filter`](https://docs.angularjs.org/api/ng/filter/filter) with the `seasonCode` property as the filter expression is one way to do it. – miqh Jul 31 '14 at 04:39
  • can u show the syntax? – spyder Jul 31 '14 at 04:49
  • In your case, something like: `activity in currentActivitiesList | filter:match.seasonCode`, where `match` is plain JS object used for filtering. Consult the official documentation on [`filter`](https://docs.angularjs.org/api/ng/filter/filter) for a good rundown of usage. – miqh Jul 31 '14 at 05:02
  • i have modified my req. please have a look n let me know – spyder Jul 31 '14 at 05:21
  • You should really provide a Plunker to make it easier for others to assist you. That said, here's a simple demonstration I whipped up matching your additional needs (nb. `$index` is available inside any `ng-repeat` to give you the current iteration index). http://plnkr.co/edit/QhQULIpdZfkBOzjL8JyZ?p=preview – miqh Jul 31 '14 at 05:47
  • http://stackoverflow.com/questions/19992090/angularjs-group-by-directive ----> this link worked like a charm – spyder Jul 31 '14 at 06:16
  • @miqid thanks for your effort mate :) – spyder Jul 31 '14 at 06:17

1 Answers1

1

please see here: http://jsbin.com/hucir/1/

  $scope.sesons = [
    {"seasonCode":1, "name":"Early Spring February - March"},
    {"seasonCode":2, "name":"Late Spring April -May"},
    {"seasonCode":3, "name":"Summer June - August"},

  ];

html:

<div ng-repeat="seson in sesons" >
    <h3>{{seson.name}}</h3>
     <div ng-repeat="activity in currentActivitiesList | filter:{seasonCode : seson.seasonCode}   " >



        <h3 style="color: green;" align="center"><bold>{{activity.seasonName}}</bold></h3><hr />    
        <div class="col-xs-4">
            <img src="images/image1.png" />
        </div>
        <div class="col-xs-8">
            <p>{{activity.activityName}}</p>      
            <p>{{activity.date}}</p>   
            <p style="color: orange;">Status: {{activity.status}}</p>
        </div>
        <div style="clear:both;"></div>

</div>
sylwester
  • 16,498
  • 1
  • 25
  • 33