[英]Angularjs ng-repeat with filter scope object
我需要根据$scope.filterObject
来过滤ng-repeat
的行。 我在$scope.customerData
中有一个数据,其中包含所有客户信息,但是在遍历每个客户时,我只想根据$scope.filterObject
显示结果。 例如,仅显示具有$scope.filterObject
工作的客户行。
这是JSFiddle 链接 。
这是我到目前为止的内容:
HTML:
<table ng-app="app" ng-controller="AppsCtrl">
<thead>
<tr>
<th>+</th>
<th>Name</th>
<th>Email</th>
<th>DOB</th>
<th>Hobby</th>
</tr>
</thead>
<tbody ng-repeat="customer in customerData">
<tr>
<td><a href="#" class="main" ng-click="expandTable($event)">+</a></td>
<td>{{customer.name}}</td>
<td>{{customer.email}}</td>
<td>{{customer.DOB}}</td>
<td>{{customer.Hobby}}</td>
</tr>
<tr class="showhide">
<td> </td>
<td>Degree</td>
<td>{{customer.Degree}}</td>
<td>Job title</td>
<td>{{customer.Job}}</td>
</tr>
<tr class="showhide">
<td></td>
<td>Country</td>
<td>{{customer.Country}}</td>
<td>City</td>
<td>{{customer.City}}</td>
</tr>
</tbody>
</table>
JS:
// AngularJS toggle table
var app = angular.module('app', []);
app.controller('AppsCtrl', function($scope) {
$scope.expandTable = function() {
console.log($(event.target).closest('tr').nextUntil("tr.main"));
$(event.target).closest('tr').nextUntil("tr.main").slideToggle();
// console.log($(event.currentTarget).parent().parent());
}
var data = [{
"name": "John",
"email": "JSmith@yahoo.com",
"DOB": "01/05/1968",
"Degree": " BSC",
"Job": "Engineer",
"Hobby": "Football",
"Country": "US",
"City": "Houston"
}, {
"name": "Jessica",
"email": "Jess@yahoo.com",
"DOB": "03/05/1976",
"Degree": " Accounting",
"Job": "CPA",
"Hobby": "Video games",
"Country": "US",
"City": "Fredericks"
}, {
"name": "Andrew",
"email": "AJoan@yahoo.com",
"DOB": "06/12/1998",
"Degree": " PHD",
"Job": "Professor",
"Hobby": "Writing",
"Country": "US",
"City": "San Diago"
}, {
"name": "Rich",
"email": "Rschol@yahoo.com",
"DOB": "09/21/1988",
"Degree": " PHD",
"Job": "Technician",
"Hobby": "Writing",
"Country": "US",
"City": "San Diago"
}];
$scope.customerData = data;
$scope.filterObject = [{
"Job": "CPA"
}, {
"Job": "Engineer"
}]
});
\\任何建议和帮助都非常感谢。 :)
您可以在ng-repeat中使用角度过滤器功能将customerArray与customerObject比较,然后仅返回与customerObject属性匹配的元素。
//Instead of array
$scope.filterObject = {
"Job": "CPA"
};
HTML:
<tr ng-repeat="customer in customerData | filter: filterObject">
否则,您可以创建一个自定义过滤器功能来比较字段或字段数组:
app.filter('customerFilter', function() {
return function( items, filterParams) {
var filtered = [];
angular.forEach(items, function(item) {
if(filterParams.Job.indexOf(item.Job) !== -1) {
filtered.push(item);
}
});
return filtered;
}; });
HTML:
<tr ng-repeat="customer in customerData | customerFilter: filters">
客户总监:
$scope.filters = {
"Job" :["Technician","CPA"]
}
这里的工作示例: https : //jsfiddle.net/Nedev_so/wb1hqeca/
$scope.customerData = data.filter(function(user){ var found = false; $scope.filterObject.forEach(function(filter){ if(filter.Job === user.Job) found = true; }) return found; });
小提琴: https : //jsfiddle.net/nandorpersanyi/yLsxqkx8/
关于Nedev的答案:假定我们正在处理大型数据集(例如$ scope.customerData有数千个客户),则DOM过滤器可能会对性能产生影响。 在这种情况下,除非需要用户与过滤器进行交互,否则最好在控制器内进行过滤。 而且,如果您的过滤器对象动态更新,只需$ watch监视其更改,然后在更改后重新呈现customerData
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.