Issue
I have three arrays in a controller:
$scope.allUsers
containing all users by:id
and:name
. E.g. smth like this.$scope.job.delegated_to
containing information related to job delegation. Looks like this.
$scope.job.delegated_to = [
{id: 5, user_id:33, user_name:"Warren", hour_count:4},
{id: 5, user_id:18, user_name:"Kelley", hour_count:2},
{id: 5, user_id:10, user_name:"Olson", hour_count:40},
{id: 5, user_id:42, user_name:"Elma", hour_count:2},
{id: 5, user_id:45, user_name:"Haley", hour_count:4},
{id: 5, user_id:11, user_name:"Kathie", hour_count:3}
]
$scope.freeUsers
which has to contain all the users, not delegated to the job.
I added a watch
$scope.$watch('job.delegated_to.length', function(){
$scope.freeUsers = filterUsers( $scope.allUsers, $scope.job.delegated_to );
});
but have not been able to construct a working filter.
Solution
Your filterUsers function would be like:
var filterUsers = function(allUsers, jobs) {
var freeUsers = allUsers.slice(0); //clone allUsers
var jobUserIds = [];
for(var ind in jobs) jobUserIds.push(jobs[ind].user_id);
var len = freeUsers.length;
while(len--){
if(jobUserIds.indexOf(allUsers[len].id) != -1) freeUsers.splice(len, 1);
}
return freeUsers;
}
Checkout fiddle http://jsfiddle.net/cQXBv/
Answered By - dubadub
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.