[英]AngularJS: Counting and Filtering on Object Partials
I have a large number of items loaded into a "tickets" object and I am trying to put several tables on the page -- one for each division so I am filtering the tickets object. 我将大量项目加载到“票证”对象中,并且尝试在页面上放置几张表-每个分区一个表,因此我正在过滤票证对象。 I can get the total number of tickets with .length but I also need the number of tickets in each division (so that I can use ng-show to show "No Tickets Found!" if there are no tickets in a specific department. 我可以获取带有.length的票证总数,但是我还需要每个部门的票证数量(这样,如果特定部门中没有票证,我可以使用ng-show来显示“找不到票证!”。
I have a plunkr that is "almost" working. 我有一个“几乎”可以正常工作的朋克 。 As you can see, there is 1 HR ticket but the "No Tickets Found!" 如您所见,其中有1张HR票证,但“未找到票证!” is still showing 仍在显示
Here's the HTML: 这是HTML:
<h3>Human Resources</h3>
<table class="table table-striped">
<tbody>
<tr>
<th>ID</th>
<th>Title</th>
<th>Department</th>
<th>Status</th>
</tr>
<tr ng-repeat="t in tickets | filter:{ Department:'HR' }">
<td>{{t.ID}}</td>
<td>{{t.Title}}</td>
<td>{{t.Department}}</td>
<td>{{t.Status}}</td>
</tr>
<tr ng-show="countDepartment('HR') == 0">
<td colspan="4"><b>No Tickets Found!</b></td>
</tr>
</tbody>
</table>
Human Resources: {{countDepartment('HR')}}
<br />
Total: {{tickets.length}}
<br /><br />
<hr />
And the JS file: 和JS文件:
var app = angular.module('app', []);
app.controller('MainController', function($scope){
$scope.tickets = [
{ID:'1', Title:'Kick Off Summer Campaign', Department:'Marketing', Status:'In Progress'},
{ID:'2', Title:'Attend SummerConn', Department:'Sales', Status:'In Progress'},
{ID:'3', Title:'Replace Ticketing Software', Department:'Support', Status:'In Progress'},
{ID:'4', Title:'Read "The Salesman Within"', Department:'Sales', Status:'In Progress'},
{ID:'5', Title:'Send Email 020', Department:'Marketing', Status:'In Progress'},
{ID:'6', Title:'Refactor Training', Department:'Support', Status:'In Progress'},
{ID:'7', Title:'Hire Call Center Coordinator', Department:'Support', Status:'In Progress'},
{ID:'8', Title:'Send Email 044', Department:'Marketing', Status:'In Progress'},
{ID:'9', Title:'Organize Convention Booths', Department:'Sales', Status:'In Progress'},
{ID:'10', Title:'Send Email 123', Department:'Marketing', Status:'In Progress'},
// Test entries for the 'Other'
{ID:'11', Title:'Network is very slow', Department:'Other', Status:'Pending'},
{ID:'12', Title:'Night Attendant has my extension wrong', Department:'HR', Status:'Pending'},
{ID:'13', Title:'Idea: We could use Twitter to advertise', Department:'Other', Status:'Pending'},
];
// Default checked state.
$scope.Department = {
Marketing: false,
Sales: false,
Support: false,
Management: false
};
$scope.checked = function(val) {
return $scope.Department[val.Department] || val.Department === 'Other';
};
$scope.countDepartment = function(val){
var cnt = 0;
for(c=0; c< $scope.tickets.length; c++){
if($scope.tickets.Department == val){
cnt = cnt+1;
} // end if
} // end for
return cnt;
};
}); // end main controller
You have a bug in your countDepartment function. 您的countDepartment函数中存在错误。 It should be 它应该是
$scope.countDepartment = function(val){
var cnt = 0;
for(c=0; c< $scope.tickets.length; c++){
if($scope.tickets[c].Department == val){
cnt = cnt+1;
} // end if
} // end for
return cnt;
};
Note that the 4th line should reference $scope.tickets[c].Department
instead of $scope.tickets.Department
. 请注意,第四行应引用$scope.tickets[c].Department
$scope.tickets.Department
而不是$scope.tickets.Department
。
Here's a slightly more compact and less error-prone way to write the same function: 这是编写相同功能的一种更紧凑,更不易出错的方法:
$scope.countDepartment = function(val){
var cnt = 0;
angular.forEach($scope.tickets, function(value, key){
if(value.Department == val)
cnt++;
});
return cnt;
};
Going down another route, you can assign filtered results to a scope variable within the ng-repeat expression, as so... 沿着另一条路线,您可以将过滤后的结果分配给ng-repeat表达式中的作用域变量,这样...
<tr ng-repeat="t in HRtickets = (tickets | filter:{ Department:'HR' })">
This variable can then be used elsewhere in your html so you can change the ng-show expression simply to... 然后可以在html的其他位置使用此变量,因此您可以将ng-show表达式更改为...
<tr ng-show="HRtickets.length == 0">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.