Mahesh G
Mahesh G

Reputation: 1276

Filtering the JSON Data in AngularJS

Can Someone help me with below Issue I am facing, My Main intention is to filter the data based on the the name and created field , I want to get the count which is having the name as IN PROGRESS and created field with this month and is there any simple way I can approach in Angular JS easily and I also now I am getting only IN PROGRESS field but in future i need to filter with other values as well.

Plunker Demo

Plunker

JS

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

app.controller("Controller", ["$scope", "$http", "$filter", "$window",
      function ($scope, $http, $filter, $window) {

        $scope.findTheValue = function () {

            $http({
                method: 'GET',
                url: 'issues.json'
            }).then(function (response) {
                $scope.selectedCount = $filter('filter')(response.data.issues, function (inputs) {
                    if (inputs.fields.status.name == 'IN PROGRESS')
                        return inputs;
                });
                console.log($scope.selectedCount.length);
                $scope.dayCount = 0;
                $scope.monthEventCount = 0;

                // Finding the The value of Day, Week and Month With Respect to Today's Date
                var todayAsBase = new Date();
                var todayAsBaseday = todayAsBase.getDay();
                var todayAsBaseWeek = getWeekNumber(todayAsBase)[0];
                var todayAsBaseMonth = todayAsBase.getMonth();
                console.log(todayAsBaseday, todayAsBaseWeek, todayAsBaseMonth);
                $scope.dayEventCount = 0;
                $scope.weekEventCount = 0;
                $scope.monthEventCount = 0;
                angular.forEach(response.data.issues, function (issue) {
                    issueDate = new Date(issue.fields.created);
                    day = issueDate.getDay();
                    week = getWeekNumber(issueDate)[0];
                    month = issueDate.getMonth();

                    if (week == todayAsBaseWeek)
                        $scope.weekEventCount = $scope.weekEventCount + 1;
                    if (month == todayAsBaseMonth)
                        $scope.monthEventCount = $scope.monthEventCount + 1;
                    if (day == todayAsBaseday && week == todayAsBaseWeek && month == todayAsBaseMonth)
                        $scope.dayEventCount = $scope.dayEventCount + 1;
                });
                console.log($scope.dayEventCount, $scope.weekEventCount, $scope.monthEventCount);
            })
        }


      }
    ]);

function getWeekNumber(d) {
    // Copy date so don't modify original
    d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
    // Set to nearest Thursday: current date + 4 - current day number
    // Make Sunday's day number 7
    d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
    // Get first day of year
    var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
    // Calculate full weeks to nearest Thursday
    var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
    // Return array of year and week number
    return [weekNo];
}

Upvotes: 0

Views: 65

Answers (1)

Judson Terrell
Judson Terrell

Reputation: 4306

 if (inputs.fields.status.name == 'IN PROGRESS') 
could be if 
(inputs.fields.status.name == $scope.selectedItem)

    $scope.selectedItem could come from a dropdown or something. I hope I understood your question correctly.

    $scope.selectedCount = $filter('filter')(response.data.issues, function(inputs) {
    if (inputs.fields.status.name == 'IN PROGRESS' 
     && created == <you need momentJs or some date compare 
     function to compare month>)
                return inputs;
            });

it would be like moment().format('MM') == moment(created).format('MM')

Upvotes: 1

Related Questions