简体   繁体   English

使用 Angularjs 按当年的月份过滤

[英]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.该项目位于 Symfony 2 和 angularjs v1.5 中。

Controller.js 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) twig 使用我的过滤器,查询是另一个使用名称搜索的过滤器(它正在工作)

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.我只是希望它通过当前年份的月份作为大纲进行过滤。

在此处输入图像描述

The syntax is wrong.语法错误。 Filter should return function.过滤器应返回 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;
  };
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM