am能夠計算結果輸入中的兩個輸入並保存結果並添加新行,但我想要計算總計結果值彙總結果(此輸入在外部TR)的如何計算表結果總結
[here is my code][1]
https://jsbin.com/gewigopevo/edit?html,js,output
am能夠計算結果輸入中的兩個輸入並保存結果並添加新行,但我想要計算總計結果值彙總結果(此輸入在外部TR)的如何計算表結果總結
[here is my code][1]
https://jsbin.com/gewigopevo/edit?html,js,output
移動
$scope.Total = total
出for loop
和地點
$scope.rvm.push({})
在的addRow1
function
你function
末應如下
$scope.addRow1 = function (index) {
$scope.rvm[index].result = $scope.rvm[index].val1 - $scope.rvm[index].val2;
var total = 0;
for (var i = 0; i < $scope.rvm.length; i++) {
total += parseInt($scope.rvm[i].result);
}
$scope.Total = total;
$scope.rvm.push({});
}
我看看你的代碼。我添加了一個總計計數器,在每次添加事件發生後計算累計總數。我將此傳遞給表格外的$scope
變量。
在控制器
$scope.outsidetotal = 0; //outside of table scope
$scope.rvm = [{}];
var total = 0;
$scope.total = 0;
$scope.addRow1 = function (index) {
$scope.rvm[index].result = $scope.rvm[index].val1 - $scope.rvm[index].val2;
$scope.rvm.push({})
$scope.outsidetotal = $scope.total += $scope.rvm[index].result;
}
嘗試使用觀看
添加以下行addRow1功能:
$scope.$watchCollection("rvm", function() {
$scope.Total = total();
});
然後創建一個函數total()並獲得所有結果值的總和。
function total() {
var totalNumber = 0;
for(var i=0; i<$scope.rvm.length; i++){
totalNumber = totalNumber + parseInt($scope.rvm[i].result);
}
return totalNumber;
}