简体   繁体   中英

How to create checkbox filter for ng-repeat list using angularjs?

I'm trying to create checkbox for ng-repeat list. I'm facing some issues.

HTML

<input type="text" ng-model="findname" placeholder="Search Name"  />
<ul>
    <li class="no-decoration" ng-repeat="tech in technologyArray">
    <label>
        <input type="checkbox" ng-model="tech.on" />{{tech.u_proffession}}
    </label>
    {{tech.on}}
    </li>
</ul>
<ul>
    <li  ng-repeat="stu in students  | filter:findname | filter:myFunc ">                
        <strong>Name :{{stu.u_name}}</strong><br />
    </li>
</ul>

JS

var ngApp = angular.module('angapp',[]);
ngApp.controller('angCtrl',function($scope,$http){
    $scope.myFunc = function(a) {
    for(tech in $scope.technologyArray){
            var t = $scope.technologyArray[tech];
            if(t.on && a.technology.indexOf(t.u_proffession) > -1){
                return true;   
            }               
        }
    };
    $scope.technologyArray = [{ u_proffession: "developer", on: false}, {u_proffession:"driver", on:false}];
    $http.get("data.php").then(function(response) {
        $scope.students= response.data.records
    });
});

JSON ARRAY

{"records":[{"u_name":"adrian","u_mail":"abc@gmail.net","u_proffession":"developer"},{...}]}

1000 Rows

The simple search ng-model="findname" is working fine when i remove the checkbox filter inside the ng-list | filter:myFunc | filter:myFunc . But when i add both of them in ng-list then no data showing in student list and also text search does not working. I wanted to use both of them.

Can anyone guide me where i'm wrong that i can fix my issue. I would like to appreciate if someone guide me. Thank You.

I am definitely not a fan of putting filter functions in controllers. In that vein, I would recommend writing an actual filter.

 angular.module('app', []) .controller('ctrl', function($scope) { $scope.technologyArray = [{ u_proffession: "developer", on: false }, { u_proffession: "driver", on: false }]; $scope.students = [{ "u_name": "adrian", "u_mail": "abc@gmail.net", "u_proffession": "developer" }, { "u_name": "adam", "u_mail": "def@gmail.net", "u_proffession": "driver" }, { "u_name": "alex", "u_mail": "ghi@gmail.net", "u_proffession": "developer" }, { "u_name": "allen", "u_mail": "jkl@gmail.net", "u_proffession": "driver" }]; }) .filter('customFilter', function() { return function(input, techs) { if(!techs || techs.length === 0) return input; var out = []; angular.forEach(input, function(item) { angular.forEach(techs, function(tech) { if (item.u_proffession === tech.u_proffession) { out.push(item); } }); }); return out; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <input type="text" ng-model="findname" placeholder="Search Name"> <ul> <li ng-repeat="tech in technologyArray"> <label> <input type="checkbox" ng-model="tech.on">{{tech.u_proffession}} </label> </li> </ul> <ul> <li ng-repeat="stu in students | filter:{u_name: findname} | customFilter:(technologyArray|filter:{on:true})"> <strong>Name :{{stu.u_name}}</strong> ({{stu.u_proffession}}) </li> </ul> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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