gpt4 book ai didi

javascript - 如何使用 angularjs 为 ng-repeat 列表创建复选框过滤器?

转载 作者:行者123 更新时间:2023-12-02 14:21:36 25 4
gpt4 key购买 nike

我正在尝试为 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 数组

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

1000 Rows

简单搜索ng-model="findname"当我删除 ng-list | filter:myFunc 内的复选框过滤器时工作正常。但是当我将它们都添加到 ng-list 中时那么 student 中没有显示任何数据列表和文本搜索不起作用。我想同时使用它们。

任何人都可以指导我哪里错了,我可以解决我的问题。如果有人指导我,我将不胜感激。谢谢。

最佳答案

我绝对不喜欢将过滤器功能放入 Controller 中。本着这种精神,我建议编写一个实际的过滤器。

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>

关于javascript - 如何使用 angularjs 为 ng-repeat 列表创建复选框过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com