动态复选框过滤器angular js操作过滤的项目

时间:2018-10-30 19:23:15

标签: angularjs checkbox filter

如何处理已过滤的项目?

例如,如果我选择Red。 它应显示如下

Value:10(5+5) Apple,Strawberry

下面是我的代码,所以请在这里提出一些解决方案。

'use strict'

angular.module('fruit', []);

function FruitCtrl($scope) {
    $scope.fruit = [
        {'name': 'Apple', 'colour': 'Red','value':5},
         {'name': 'Strawberry', 'colour': 'Red','value':5},
        {'name': 'Orange', 'colour': 'Orange','value':5},
        {'name': 'Orange1', 'colour': 'Orange','value':5},
        {'name': 'Banana', 'colour': 'Yellow','value':5},
          {'name': 'Banana1', 'colour': 'Yellow','value':5}];
    
    $scope.colourIncludes = [];
    
    $scope.includeColour = function(colour) {
        var i = $.inArray(colour, $scope.colourIncludes);
        if (i > -1) {
            $scope.colourIncludes.splice(i, 1);
        } else {
            $scope.colourIncludes.push(colour);
        }
    }
    
    $scope.colourFilter = function(fruit) {
        if ($scope.colourIncludes.length > 0) {
            if ($.inArray(fruit.colour, $scope.colourIncludes) < 0)
                return;
        }
        
        return fruit;
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="fruit">
    <div ng-controller="FruitCtrl">
        
        <input type="checkbox" ng-click="includeColour('Red')"/> Red</br/>
        <input type="checkbox" ng-click="includeColour('Orange')"/> Orange</br/>
        <input type="checkbox" ng-click="includeColour('Yellow')"/> Yellow</br/>
        
        <ul>
            <li ng-repeat="f in fruit | filter:colourFilter">
                {{f.name}}
            </li>
        </ul>

        Filter dump: {{colourIncludes}}
    </div>
</div>

预先感谢

1 个答案:

答案 0 :(得分:0)

在控制器中创建一个函数,您可以在其中使用复选框的状态计算所选产品的总价值并将其绑定到html。