angular.module('ASP').controller('ObjectiveKpiScore',['$scope','$http','KPISetup',function($scope,$http,KPISetup){ $scope.listkpis=[]; // for get data in first coming this page KPISetup.manageObjectiveKpiScore.query().$promise.then(function(data){ $scope.listkpis=data.listkpis; console.log($scope.listkpis); }); $scope.calColumnScore = function(col){ chkNum() var result = 0 if($scope.listkpis.length > 0){ if(col == "up1"){ result = $scope.listkpis.map(obj => obj.score_up1).reduce(cal) }else if(col == "up2"){ result = $scope.listkpis.map(obj => obj.score_up2).reduce(cal) }else if (col == "low1") { result = $scope.listkpis.map(obj => obj.score_low1).reduce(cal) }else if (col == "low2") { result = $scope.listkpis.map(obj => obj.score_low2).reduce(cal) } } return result } function cal(total, num){ return total + num } function chkNum(){ $("input[type='number']").on( "keypress", function( event ) { // a-z [97-122] // A-Z [65-90] // 0-9 [48-57] // Enter [13] // Backspace [8] // _ [95] if( !(event.which >= 48 && event.which <= 57) && event.which !== 8){ event.preventDefault(); } }) } $scope.save = function(){ if(chkTotalScore()){ alert("คะแนนรวมแต่ละระดับต้องเท่ากับ 100 คะแนน!!"); }else if(chkScore()){ alert("คะแนนแต่ละหัวข้อต้องไม่เท่ากับ 0 คะแนน!!"); }else if (confirm(MyCode[1])){ $scope.sendparam = { kpiscore : $scope.listkpis } KPISetup.manageObjectiveKpiScore.save(JSON.stringify($scope.sendparam)).$promise.then(function(){ alert(MyCode[18]); }); } } function chkScore(){ var result = false if($scope.listkpis.length > 0){ $scope.listkpis.forEach(function(data){ if( (data.score_low1 == 0) || (data.score_low2 == 0) || (data.score_up1 == 0) || ( data.score_up2 == 0) ){ result = true } }) } return result } function chkTotalScore(){ var result = false var score1 = parseInt($scope.calColumnScore("up1")) var score2 = parseInt($scope.calColumnScore("up2")) var score3 = parseInt($scope.calColumnScore("low1")) var score4 = parseInt($scope.calColumnScore("low2")) if( (score1 != 100) || (score2 != 100) || (score3 != 100) || ( score4 != 100) ){ result = true } return result } }]);