角度范围过滤器不工作

时间:2016-05-28 11:02:51

标签: angularjs checkbox filter angular-filters

我已经制作了价格范围过滤器,当点击复选框时,我只想显示那些价格落在复选框指定的价格范围内的商品

这就是我所关注的 - http://jsfiddle.net/65Pyj/

HTML

<div class="checkbox">
    <input type="checkbox" ng-click="includePrice('0,700')" ng-checked=""/> Rs 700 and Below <br/>
    <input type="checkbox" ng-click="includePrice('701,1500')" ng-checked=""/> Rs 701 - 1500 <br/>
    <input type="checkbox" ng-click="includePrice('1501,3000')" ng-checked=""/> Rs 1501 - 3000 <br/>
     <input type="checkbox" ng-click="includePrice('3001,5000')" ng-checked=""/> Rs 3000 - 5000 <br/>
     <input type="checkbox" ng-click="includePrice('5001,100000000')" ng-checked=""/> Rs 5001 and Above
</div>

在控制器中,我将每个复选框的最小值和最大值分别放入一个数组中,并将该数组的最小值和最大值再次作为下限和上限

控制器

$scope.priceIncludes = [];
$scope.ranges = [];

$scope.includePrice = function(pricerange) {
    var i = $.inArray(pricerange, $scope.priceIncludes);
    if (i > -1) {
        $scope.priceIncludes.splice(i, 1);
        ranges = pricerange.split(',').splice(i, 1);
    } else {
        $scope.priceIncludes.push(pricerange);
    }
    var arrayString = $scope.priceIncludes.join();
    var rangeArray = arrayString.split(',')
    $scope.maxRange = function( rangeArray ){
        return Math.max.apply( Math, rangeArray );
    };
    $scope.minRange = function( rangeArray ){
        return Math.min.apply( Math, rangeArray );
    };
    $scope.ranges[1] = $scope.maxRange(rangeArray);
    $scope.ranges[0] = $scope.minRange(rangeArray);
}

$scope.priceFilter = function(searchResult) {
    if ($scope.priceIncludes.length > 0) {
        if ((parseInt(searchResult.fees) >= parseInt($scope.ranges[0])) && (parseInt(searchResult.fees) <= parseInt($scope.ranges[1])))
            return;
    }
    return searchResult;
}

当我使用

filter:priceFilter

它会返回超出所选最小和最大限制的随机结果。

1 个答案:

答案 0 :(得分:1)

如果条件为真,您应该返回值。 不是相反。

$scope.priceFilter = function(searchResult) {
        if ($scope.priceIncludes.length > 0) {
            if ((parseInt(searchResult.fees) >= parseInt($scope.ranges[0])) && (parseInt(searchResult.fees) <= parseInt($scope.ranges[1])))
        return searchResult;
        } else {
        return searchResult;
        }
    }