按多个值angularjs过滤

时间:2016-06-07 01:10:10

标签: angularjs

我想执行过滤器并获取js文件中结果行的计数

过滤

1)locationId和
 2)在IntroductoryPeriodStart和introductionctoryPeriodEnd之间的startDate。

以下语句给出了synatx错误。有人可以给我正确的陈述。

$scope.NewEmps = $filter('filter')($scope.Summary.CorpEmployees, { locationId: $scope.selectedItem.Label } | { startDate:$scope.Model.introductoryPeriodStart:$scope.Model.introductoryPeriodEnd}).length;

代码:

DashBoardModule.controller('DashBoardController', ['$scope','$filter', 'DashBoardModuleService', function ($scope,$filter, DashBoardModuleService) {

    $scope.SelectedItems = {};
    $scope.NewEmps;
    $scope.Qualfd;
    $scope.Scred;
    $scope.submtd8850MisinDocs;
    $scope.DocsRecvd8850;
    $scope.DocsSubmtd8850;
    $scope.Approved;
    $scope.Denied;
    $scope.NeedMoreInfo;
    $scope.selectedItem;
var Ein = '0000000';

    DashBoardModuleService.GetDashBoardSummary(Ein).then(function (response) {
       // $scope.Summary = JSON.stringify(response.data.Summary);
        $scope.Summary = response.data.Summary;
        $scope.corporateId = response.data.Summary.corporateId;       
        $scope.dropboxitemselected = function (item) {
            $scope.selectedItem = item;          
        }     
        console.log($scope.Summary);
    });

    $('#txtDateRange').on('apply.daterangepicker', function (ev, picker) {
        $scope.isRefreshing = true;
        $scope.Model.introductoryPeriodEnd = $filter('date')(new Date(picker.endDate), 'MM/dd/yyyy');
        $scope.Model.introductoryPeriodStart = $filter('date')(new Date(picker.startDate), 'MM/dd/yyyy');
        $scope.Model.typeAvailability = picker.chosenLabel === "Custom Range" ? "Custom" : picker.chosenLabel;
        $scope.$apply();

        console.log($scope.Model.introductoryPeriodEnd);
        console.log($scope.Model.introductoryPeriodStart);
        $scope.FilterByDate();
    });   

    angular.element(document).ready(function () {
        var dateConfiguration = new Date();
        $('#txtDateRange').daterangepicker({
            locale:{
                format: 'MM/DD/YYYY'
            },
            autoApply: true
        });
        loadChartCallCenter();
        loadChartHumanEfits();
        loadChartQualificationSource(362, 100, 88);
    });

    $scope.greaterThan = function (prop, val) {
        return function (item) {
            return item[prop] > val;
        }
    };

    $scope.lessThan = function (prop, val) {
        return function (item) {
            return item[prop] < val;
        }
    };

    $scope.FilterByDate = function () {
        console.log($scope.selectedItem);
        console.log($scope.selectedItem.Label);
        console.log($scope.Model.introductoryPeriodStart);
        console.log($scope.Summary.CorpEmployees);

        $scope.NewEmps = $scope.$eval("Summary.CorpEmployees | filter:{locationId:selectedItem.Label} | filter:greatherThan('startDate',Model.introductoryPeriodStart) | filter:lessThan('startDate',Model.introductoryPeriodEnd)").length;
        }
    };
}]);

1 个答案:

答案 0 :(得分:2)

你有几个错误:  0-未定义日期(更好/更少)过滤器。  1-你不能这样使用。

所以:  0-定义更好的

$scope.greaterThan = function(prop, val){
  return function(item){
    return item[prop] > val;
  }
};

1-定义lessThan

$scope.lessThan = function(prop, val){
  return function(item){
    return item[prop] < val;
  }
};

2-使用$scope.$eval

$scope.size =  $scope.$eval("elements | filter:{locationId:location} | filter:greatherThan('startDate',initDate) | filter:lessThan('startDate',endDate)").length;

我用更好的方法更新了。检查一下:

我将lessThangreatherThan合并为一个inRangeDate。另外,如果它不是我将自动投射到的日期。

$scope.inRangeDate = function (prop, init, end) {
  return function(item){
    init = init instanceof Date ? init : new Date(init);
    end = end instanceof Date ? end : new Date(end);
    return item[prop] > init && item[prop] < end;
  }
};

检查此Plunkr:http://plnkr.co/edit/bxPdNAUjV6I0uuJb8iRp?p=preview(我将其应用于$scope.size) 0.第一种方法是Plunkr的第4版 1. inRangeDate方法在同一个Plunkr的第5版