gpt4 book ai didi

javascript - Angularjs ng-repeat 与过滤范围对象

转载 作者:行者123 更新时间:2023-11-28 18:19:26 25 4
gpt4 key购买 nike

我需要过滤 ng-repeat 中的行基于 $scope.filterObject 中的内容。我有一个数据在 $scope.customerData包含所有客户信息,但是当循环每个客户时,我只想根据 $scope.filterObject 中的内容显示结果。例如,仅显示具有 $scope.filterObject 中职位的客户行.

这是 JSFiddle link .

这是我到目前为止所拥有的:

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"
}]
});

\非常感谢任何建议和帮助。 :)

最佳答案

您可以使用 Angular filter 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">

客户 Controller :

$scope.filters = {
"Job" :["Technician","CPA"]
}

这里的工作示例:https://jsfiddle.net/Nedev_so/wb1hqeca/

关于javascript - Angularjs ng-repeat 与过滤范围对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40142352/

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