我有一个由员工详细信息组成的对象数组,例如薪水,姓名,部门和ID。

JS:

$scope.filteredDept = [];

$scope.employees = [
    {'id':1, 'dept': 'IT', 'sal': 5000, 'name': 'ramesh'},
    {'id':2, 'dept': 'IT', 'sal': 4000, 'name': 'Girish'},
    {'id':3, 'dept': 'Management', 'sal': 10000, 'name': 'Kartik'},
    {'id':4, 'dept': 'Marketing', 'sal': 8000, 'name': 'Shenoy'},
    {'id':5, 'dept': 'Marketing', 'sal': 7000, 'name': 'Darvik'},
    {'id':6, 'dept': 'Management', 'sal': 17000, 'name': 'Ravi Kumar'}
];
$scope.empFiltered = $scope.employees;
$scope.departments = [
    {'id':1, 'name':'IT','selected':false},
    {'id':2, 'name':'Management','selected':false},
    {'id':3, 'name':'Marketing','selected':false}
];

$scope.sortEmp = function(dept_id){
    $scope.bool = $scope.departments.filter(function(e){
        if(e.id === dept_id){
            var idx = $scope.filteredDept.indexOf(e.name,0);
            if(idx === -1){
                $scope.filteredDept.push(e.name);
                $scope.filterEmpList();
                return 0;
            }
            else {
                $scope.filteredDept.splice(idx,1);
                $scope.filterEmpList();
                return 0;
            }
        }
    });
};
$scope.filterEmpList = function(){
    $scope.filteredData = [];
    if($scope.filteredDept.length !==0){
        $scope.empFiltered = [];
        angular.forEach($scope.filteredDept, function(val,key){
            $scope.filteredData = $scope.employees.filter(function(e){
                if(e.dept === val){
                 return e;
                }
            });
            angular.forEach($scope.filteredData, function(v,k){
                $scope.empFiltered.push(v);
            });
        });
    }
    else{
        $scope.empFiltered = $scope.employees;
    }
};


HTML:

Hike percentage : <input type="number" ng-model="hike"/>
<button>Hike the salary</button>
<br>
Hike by dept : <br>
<div class="checkbox" ng-repeat="dept in departments track by $index">
  <label>
    <input type="checkbox" ng-model="dept.selected" ng-click="sortEmp(dept.id)"/> {{dept.name}}
  </label>
</div>
<table border="1">
  <thead>
      <tr>
          <th>Name</th>
            <th>Dept</th>
            <th>Hike Percentage</th>
            <th>Hike Amount</th>
            <th>Salary</th>
        </tr>
        <tr ng-repeat="emp in empFiltered track by $index">
          <td>{{emp.name}}</td>
            <td>{{emp.dept}}</td>
            <td>hike percentage</td>
            <td>hike amount</td>
            <td>{{emp.sal | currency: '&#8360; '}}</td>
        </tr>
    </thead>
</table>


在这里,我正在选择部门来基于选定的复选框来过滤雇员表。

我想根据部门和输入框中的加薪百分比来增加薪水。

我不知道如何实现它。我可以得到任何帮助或指导吗?

最佳答案

您可以调用一个函数来提高员工的薪水,然后应用地图



var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {

   	$scope.filteredDept = [];

    $scope.employees = [
    	{'id':1, 'dept': 'IT', 'sal': 5000, 'name': 'ramesh'},
        {'id':2, 'dept': 'IT', 'sal': 4000, 'name': 'Girish'},
        {'id':3, 'dept': 'Management', 'sal': 10000, 'name': 'Kartik'},
        {'id':4, 'dept': 'Marketing', 'sal': 8000, 'name': 'Shenoy'},
        {'id':5, 'dept': 'Marketing', 'sal': 7000, 'name': 'Darvik'},
        {'id':6, 'dept': 'Management', 'sal': 17000, 'name': 'Ravi Kumar'}
    ];
    $scope.empFiltered = $scope.employees;
    $scope.departments = [
    	{'id':1, 'name':'IT','selected':false},
        {'id':2, 'name':'Management','selected':false},
        {'id':3, 'name':'Marketing','selected':false}
    ];

    $scope.sortEmp = function(dept_id){
    	$scope.bool = $scope.departments.filter(function(e){
        	if(e.id === dept_id){
                var idx = $scope.filteredDept.indexOf(e.name,0);
                if(idx === -1){
                	$scope.filteredDept.push(e.name);
                    $scope.filterEmpList();
                    return 0;
                }
                else {
                	$scope.filteredDept.splice(idx,1);
                    $scope.filterEmpList();
                    return 0;
                }
            }
        });
    };
    $scope.filterEmpList = function(){
    	$scope.filteredData = [];
		if($scope.filteredDept.length !==0){
        	$scope.empFiltered = [];
        	angular.forEach($scope.filteredDept, function(val,key){
            	$scope.filteredData = $scope.employees.filter(function(e){
                    if(e.dept === val){
                     return e;
                    }
                });
				angular.forEach($scope.filteredData, function(v,k){
                	$scope.empFiltered.push(v);
                });
            });
        }
        else{
        	$scope.empFiltered = $scope.employees;
        }
    };
    $scope.hikeSal = function(){
    	var arr = [];
        arr = $scope.empFiltered.map(function(e){
        	if(e.hike === undefined){
            	e.hike = 0;
            }
        	e.hike = parseInt(e.hike) + $scope.hike;
            e.hikeAmt = e.sal * ($scope.hike * 0.01);
        	e.sal += e.sal * ($scope.hike * 0.01);
            //return e;
        });
    };
});

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
ercentage : <input type="number" ng-model="hike"/>
<button ng-click="hikeSal()">Hike the salary</button>
<br>
Hike by dept : <br>
<div class="checkbox" ng-repeat="dept in departments track by $index">
  <label>
  	<input type="checkbox" ng-model="dept.selected" ng-click="sortEmp(dept.id)"/> {{dept.name}}
  </label>
</div>
<table border='1'>
	<thead>
    	<tr>
        	<th>Name</th>
            <th>Dept</th>
            <th>Hike Percentage</th>
            <th>Hike Amount</th>
            <th>Salary</th>
        </tr>
        <tr ng-repeat="emp in empFiltered track by $index">
        	<td>{{emp.name}}</td>
            <td>{{emp.dept}}</td>
            <td>{{emp.hike}}</td>
            <td>{{emp.hikeAmt}}</td>
            <td>{{emp.sal | currency: '&#8360; '}}</td>
        </tr>
    </thead>
</table>
</div>
</body>
</html>





希望能帮助到你

关于javascript - 如何根据AngularJS的部门来提高员工的薪水?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46910809/

10-10 21:43