angularjssymfony-2.5

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


Solution

  • 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;
      };
    };