Reputation: 3531
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":"[email protected]","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
. 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.
Upvotes: 0
Views: 2690
Reputation: 7194
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": "[email protected]",
"u_proffession": "developer"
}, {
"u_name": "adam",
"u_mail": "[email protected]",
"u_proffession": "driver"
}, {
"u_name": "alex",
"u_mail": "[email protected]",
"u_proffession": "developer"
}, {
"u_name": "allen",
"u_mail": "[email protected]",
"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>
Upvotes: 2