2016-10-22 96 views
0

我有這樣一個JSON,我想它的標誌值集團和領域

$scope.staffs = [ 

     { 
      "id": 1, 
      "name": "Management", 
      "Flag": 1 
     }, 
     { 
      "id": 2, 
      "name": "Revenue Collection/Ledger", 
      "Flag": 1 
     }, 
     { 
      "id": 3, 
      "name": "Office Assistance", 
      "Flag": 1 
     }, 
     { 
      "id": 4, 
      "name": "Operators (Source)", 
      "Flag": 2 
     }, 
     { 
      "id": 5, 
      "name": "Operators (WTP)", 
      "Flag": 2 
     }, 
     { 
      "id": 6, 
      "name": "Operators at Networks", 
      "Flag": 2 
     } 
    ] 

計算字段的總和,組和我的HTML是這樣在這裏我想要什麼當用戶把價值常駐現場應該找到它標誌值,並計算常駐領域的總和與標誌值,如果標誌值是1個放總和wspStaffTotal.Admin_Perm_Total,如果標誌值是2將總和wspStaffTotal .Technical_Perm_Total

<div ng-repeat="wspStaffTbl in staffs"> 
{{ wspStaffTbl.name }} 
<input type="text" ng-model="wspStaffTbl.Permanent" ng-change="updatePermanentTotal()"> 
</div> 

<input type="text" ng-model="wspStaffTotal.Admin_Perm_Total"> 
<input type="text" ng-model="wspStaffTotal.Technical_Perm_Total"> 

是我的嘗試是

$scope.updatePermanentTotal = function(){ 
    $scope.wspStaffTotal.Admin_Perm_Total = 0; 
    $scope.wspStaffTotal.Technical_Perm_Total = 0; 

    angular.forEach($scope.staffs, function(value, key){ 

    if(!isNaN(parseInt(value.Permanent))){ 
     if(value.Flag==1){ 
      $scope.wspStaffTotal.Admin_Perm_Total = $scope.wspStaffTotal.Admin_Perm_Total + parseInt(value.Permanent); 
      } 
    if(value.Flag==2){ 
      $scope.wspStaffTotal.Technical_Perm_Total = $scope.wspStaffTotal.Technical_Perm_Total + parseInt(value.Permanent); 
      } 
    } 
}) 
} 

但預計它無法正常工作。

+0

運行在每此功能陳ge會花費你很多的表現... –

+0

是啊我知道有沒有其他方式? – sanu

+0

反跳才能解決您的性能問題... https://lodash.com/docs/4.16.4#debounce –

回答

2

我希望這是你希望的東西...

app.js

var myApp = angular.module('test' , []); 
myApp.controller('test_ctrl' , function($scope){ 
    $scope.staffs = [ 

     { 
      "id": 1, 
      "name": "Management", 
      "Flag": 1 
     }, 
     { 
      "id": 2, 
      "name": "Revenue Collection/Ledger", 
      "Flag": 1 
     }, 
     { 
      "id": 3, 
      "name": "Office Assistance", 
      "Flag": 1 
     }, 
     { 
      "id": 4, 
      "name": "Operators (Source)", 
      "Flag": 2 
     }, 
     { 
      "id": 5, 
      "name": "Operators (WTP)", 
      "Flag": 2 
     }, 
     { 
      "id": 6, 
      "name": "Operators at Networks", 
      "Flag": 2 
     } 
    ]; 

    $scope.updatePermanentTotal = function(){ 
     $scope.wspStaffTotal = { 
      Admin_Perm_Total: 0, 
      Technical_Perm_Total :0 
     } 

     angular.forEach($scope.staffs, function(value, index){ 

      if(!isNaN(parseInt(value.Permanent))){ 
       if(value.Flag==1){ 
        $scope.wspStaffTotal.Admin_Perm_Total = $scope.wspStaffTotal.Admin_Perm_Total + parseInt(value.Permanent); 

       } 
       if(value.Flag==2){ 
        $scope.wspStaffTotal.Technical_Perm_Total = $scope.wspStaffTotal.Technical_Perm_Total + parseInt(value.Permanent); 
       } 
      }else{ 
       console.log("value is NAN"); 
      } 

     }); 
     alert("result flag1:{" +$scope.wspStaffTotal.Admin_Perm_Total+"} flag2: {" +$scope.wspStaffTotal.Technical_Perm_Total +"}"); 
    } 
}); 

HTML

<html ng-app="test"> 

<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 
    <script src="app.js"></script> 

</head> 
<body ng-controller="test_ctrl"> 
<div ng-repeat="wspStaffTbl in staffs"> 
    {{ wspStaffTbl.name }} 
    <input type="text" ng-model="wspStaffTbl.Permanent" ng-change="updatePermanentTotal()"> 
</div> 

<input type="text" ng-model="wspStaffTotal.Admin_Perm_Total"> 
<input type="text" ng-model="wspStaffTotal.Technical_Perm_Total"> 

</body> 
</html> 
+0

什麼是你的代碼和我的不同? – sanu

+0

$ scope.wspStaffTotal = { Admin_Perm_Total:0, Technical_Perm_Total:0 } 使其工作 你試試我的嗎? –

+0

它正在工作。怎麼樣? – sanu

0

首先使永久值在控制器的陣列

$scope.webStaffTbl.Permanent = []; 

然後傳遞這些將值給該函數:

<input type="text" ng-model="wspStaffTbl.Permanent[$index]" ng-change="updatePermanentTotal(wspStaffTbl.Permanent[$index],wspStaffTbl.flag)"> 

,並用它的標誌的永久值傳遞給函數

然後在$ scope函數中這樣做:

$scope.updatePermanentTotal = function(permanentValue,flag){ 


$scope.wspStaffTotal.Admin_Perm_Total = 0; 
    $scope.wspStaffTotal.Technical_Perm_Total = 0; 

    angular.forEach($scope.staffs, function(value, key){ 

    if(!isNaN(parseInt(permanentValue))){ 
     if(flag==1){ 

      $scope.wspStaffTotal.Admin_Perm_Total = $scope.wspStaffTotal.Admin_Perm_Total + parseInt(permanentValue); 
      } 
    if(flag==2){ 

      } 
    } 
}) 
}