AngularJS:总计等于所有匹配对象的总和
我有一个由一系列任务组成的请求数组。我需要每个请求的持续时间字段等于与每个请求关联的任务的持续时间字段的总和,并且能够对任务中的更改做出反应 我尝试过angular.forEach,甚至只是一个javascript for循环。我认为有一个角度简单的概念,我没有做到这一点。请告诉我怎么做 我从这里开始,但意识到在将任务添加到一起之前,我无法通过适用的Req_ID筛选任务——我需要一个WHERE子句(LOL): 这是我的控制器:AngularJS:总计等于所有匹配对象的总和,angularjs,Angularjs,我有一个由一系列任务组成的请求数组。我需要每个请求的持续时间字段等于与每个请求关联的任务的持续时间字段的总和,并且能够对任务中的更改做出反应 我尝试过angular.forEach,甚至只是一个javascript for循环。我认为有一个角度简单的概念,我没有做到这一点。请告诉我怎么做 我从这里开始,但意识到在将任务添加到一起之前,我无法通过适用的Req_ID筛选任务——我需要一个WHERE子句(LOL): 这是我的控制器: var app = angular.module('app',[])
var app = angular.module('app',[]);
app.controller('MainController', function($scope){
$scope.selRequest = 1;
$scope.Requests = [
{id: 1, Title: 'Make a web page', Duration: 0, TOR: 0},
{id: 2, Title: 'Make a web service', Duration: 0, TOR: 0},
{id: 3, Title: 'Make a web application', Duration: 0, TOR: 0}
];
$scope.Tasks = [
{id: 1, Req_ID: 1, Title: 'Graphic Prototype', Duration: .5, TOT: 0},
{id: 2, Req_ID: 1, Title: 'Develop CSS', Duration: 3.5, TOT: 0},
{id: 3, Req_ID: 1, Title: 'Code in Editor', Duration: 6, TOT: 0},
{id: 4, Req_ID: 2, Title: 'Write Stored Procedures', Duration: 10, TOT: 0},
{id: 5, Req_ID: 2, Title: 'Build API', Duration: 6, TOT: 0},
{id: 6, Req_ID: 3, Title: 'Build Static App', Duration: 12, TOT: 0},
{id: 7, Req_ID: 3, Title: 'Build Data Store', Duration: 4, TOT: 0},
{id: 8, Req_ID: 3, Title: 'Add Functionality to Static App', Duration: 6, TOT: 0}
];
$scope.showKids = function(id){
$scope.selRequest = id;
} // end fn
}); // end MainController
我创建了一个简单的视图来显示相关的任务,以测试请求分配的持续时间
<!DOCTYPE html>
<html ng-app="app">
<head>
<link data-require="bootstrap-css@3.1.1" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
<script data-require="bootstrap@*" data-semver="3.1.1" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script data-require="ui-bootstrap@*" data-semver="0.10.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="MainController">
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Title</th>
<th>Duration</th>
<th>TOR</th>
</tr>
<tr ng-repeat="req in Requests">
<td>{{req.id}}</td>
<td><a href="" ng-click="showKids(req.id)">{{req.Title}}</a></td>
<td>{{req.Duration}}</td>
<td>{{req.TOR}}</td>
</tr>
</table>
<br /><br />
<table class="table table-striped">
<tr>
<th>ID</th>
<th>REQ</th>
<th>Title</th>
<th>Duration</th>
<th>TOT</th>
</tr>
<tr ng-repeat="t in Tasks | filter: {Req_ID: selRequest}">
<td>{{t.id}}</td>
<td>{{t.Req_ID}}</td>
<td>{{t.Title}}</td>
<td><input type="text" style="width:50px;" name="Duration" data-ng-model="t.Duration" /></td>
<td><input type="text" style="width:50px;" name="TOT" data-ng-model="t.TOT" /></td>
</tr>
</table>
</body>
</html>
身份证件
标题
期间
托尔
{{req.id}
{{req.Duration}
{{req.TOR}
身份证件
请求
标题
期间
托特
{{t.id}
{{t.Req_ID}
{{t.Title}}
我对你要找的东西做了一些假设。基本上,这里的关键是您可以将
ng change
添加到要跟踪的表单变量中,然后在该更改事件运行时调用函数重新计算总计。步骤如下所示:
向相关字段添加ng更改
<td><input type="text" style="width:50px;" name="Duration" data-ng-model="t.Duration" data-ng-change="updateDuration(t.Req_ID)"/></td>
<td><input type="text" style="width:50px;" name="TOT" data-ng-model="t.TOT" data-ng-change="updateTotals(t.Req_ID)" /></td>
添加例程以更新总计
<td><input type="text" style="width:50px;" name="Duration" data-ng-model="t.Duration" data-ng-change="updateDuration(t.Req_ID)"/></td>
<td><input type="text" style="width:50px;" name="TOT" data-ng-model="t.TOT" data-ng-change="updateTotals(t.Req_ID)" /></td>
为了简洁起见,我只展示一个。它们都在下面的plunker中,但基本相同
$scope.updateDuration = function (sumId) {
var sum = 0;
for (var idx = 0; idx < $scope.Tasks.length; idx++) {
if ($scope.Tasks[idx].Req_ID === sumId){
sum += $scope.Tasks[idx].Duration * 1; // simple cast as int
}
}
for (idx = 0; idx < $scope.Requests.length; idx++) {
if ($scope.Requests[idx].id == sumId) {
$scope.Requests[idx].Duration = sum;
}
}
};
$scope.updateDuration=函数(sumId){
var总和=0;
对于(变量idx=0;idx<$scope.Tasks.length;idx++){
if($scope.Tasks[idx].Req\u ID==sumId){
sum+=$scope.Tasks[idx].Duration*1;//简单强制转换为int
}
}
对于(idx=0;idx<$scope.Requests.length;idx++){
if($scope.Requests[idx].id==sumId){
$scope.Requests[idx]。持续时间=总和;
}
}
};
查看更新的Plunker
我不太清楚-您只对
工期
列求和-而不是TOT
?我最终会做任务时间(TOT),但由于它的功能与工期相同,我想我会根据答案自己做。我想我得到了您想要的,包括两个字段。我还在范围初始化时添加了一个调用,以设置总数。希望有帮助!这正是我所需要的。我将在服务中设置数据加载的初始值,但随着工作的完成,表单将完美地工作。当然,ng disabled很方便,所以员工不能延长自己的截止日期(LOL)。非常感谢您抽出时间!