我正在尝试为ng-repeat列表创建复选框。我正面临一些问题。
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
当我删除ng-list ng-model="findname"
中的复选框过滤器时,简单搜索| filter:myFunc
正常工作。但是当我在ng-list
中添加它们时,student
列表中的数据和文本搜索中的数据都不起作用。我想用它们两个。
任何人都可以指导我,我可以解决我的问题。如果有人指导我,我很感激。谢谢。
答案 0 :(得分:2)
我绝对不喜欢将过滤器功能放在控制器中。在这方面,我建议写一个实际的过滤器。
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;
}
});
&#13;
<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>
&#13;