按日期季度进行角度自定义过滤

时间:2015-05-26 21:52:12

标签: javascript angularjs angularjs-ng-repeat angularjs-filter

我在页面上有一系列对象,重复了一遍。我还在下拉选择框中列出了日期季度列表。如果在下拉列表中选择的季度比ng重复列表中的项目晚,那么该项目应该被过滤掉。

不幸的是,我无法让它发挥作用。这是我的HTML:

<div ng-app="programApp" ng-controller="programController">
  <select ng-model="advancedFiltersy">
    <option ng-repeat="indexitem in quarters">{{indexitem}}</option>
  </select>
  <div ng-repeat="item in listing | advFilterName:advancedFiltersy:quarters">
    {{item.name}}
  </div>
</div>

这是我的角色剧本:

angular.module('programApp', ['programApp.controllers','programApp.filters']);
angular.module('programApp.controllers', [])
    .controller('programController', ['$scope', '$filter', 
    function($scope, $filter){
      $scope.advancedFiltersy = 'zeroeth';
      $scope.quarters = ['zeroeth','first','second','third','fourth','fifth','sixth'];
      $scope.listing = [{'name':'aa','aacApprovalIconQuarter':'zeroeth'
      },{'name':'ab','aacApprovalIconQuarter':'first'
      },{'name':'ac','aacApprovalIconQuarter':'second'
      },{'name':'ad','aacApprovalIconQuarter':'third'
      },{'name':'ae','aacApprovalIconQuarter':'fourth'
      },{'name':'af','aacApprovalIconQuarter':'fifth'
      },{'name':'ag','aacApprovalIconQuarter':'sixth'}];
    }]);

angular.module('programApp.filters', []).filter('advFilterName', function(){
    return function(entries, advancedFiltersy, quarters){
        var advFiltered = [];
        angular.forEach(entries, function (entry){
            if(quarters.indexOf(advancedFiltersy) > quarters.indexOf(entry.aacApprovalIconQuarter)){
            }else{
                advFiltered.push(entry);
            };
        });
    };
});

所有重复项目都没有显示,因此过滤器无法正常工作。我需要做些什么来使过滤器工作?

以下是Codepen:http://codepen.io/trueScript/pen/MwbVpO

1 个答案:

答案 0 :(得分:1)

您仍需要返回已过滤结果的值:

angular.module('programApp.filters', []).filter('advFilterName', function(){
    return function(entries, advancedFiltersy, quarters){
        var advFiltered = [];
        angular.forEach(entries, function (entry){
            if(quarters.indexOf(advancedFiltersy) > quarters.indexOf(entry.aacApprovalIconQuarter)){
            }else{
                advFiltered.push(entry);
            };
        });
        // Fix
        return advFiltered;
    };
});

修正了这里:http://codepen.io/anon/pen/LVbdQo?editors=101