0

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];
}
1
  • Why not try to use lodash to do this kind of logic? Commented Aug 23, 2017 at 3:23

1 Answer 1

1
 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')

Sign up to request clarification or add additional context in comments.

2 Comments

Sure let me try this way
Did this help you ?

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.