Filter by the month of the current year with Angularjs

I have a screen with accounts payable and I would like to filter each account by month, the current month should be loaded first and have the options of Previous and Next to navigate between the months, before and after.

The project is in Symfony 2 with angularjs v1.5.

Controller.js

$scope.selectedMonth = '';

$scope.selectedMonthFilter = function(created_at) {
    console.log(created_at);

    var d = new Date(created_at);
    if(!$scope.selectedMonth) return true;
    return d.getMonth() === $scope.selectedMonth;
};

twig with my filter, the query is another filter that searches with a name (it’s working)

ng-repeat="income in incomes | filter:query:selectedMonthFilter(income.create_at) | orderBy: 'income.id'"

The error is that it does not search for the current year, the console shows “undefined”, that’s all.

I just want it to filter through the month of the current year as the outline.

enter image description here

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The syntax is wrong. Filter should return function.

Fix:

ng-repeat="income in incomes | filter:selectedMonthFilter(criteria) | orderBy: 'income.id'"

//Filter

$scope.selectedMonth = "";

$scope.selectedMonthFilter = function (criteria) {
 console.log(criteria) // matching criteria
  return function (income) {
    console.log(income);
    var d = new Date(income.created_at);
    if (!$scope.selectedMonth) return true;
    return d.getMonth() === $scope.selectedMonth;
  };
};

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply