LegionDev LegionDev - 3 months ago 8
AngularJS Question

How to create drop down lists from an array, that excludes a certain option - AngularJs

Ok so I have this table which lists all employees and their supervisors in a dropdown.
(the first two options in the drop down are always there the other are there depending weather or not they have been selected as a supervisor in the Supervisor Column)

EmployeeTable

Now the issue I'm having is someone can't be selected as his/her own supervisor

on the HTML the table rows are simply created using

ng-repeat="employeeInfo in employees"


and the Drop down select options are loaded like this

ng-options="supervisor.id as (supervisor.name + ' ' + supervisor.lastname + ' ' + supervisor.id) for supervisor in supervisorList"


how do I exclude the person himself in the drop down of available supervisors ?

ddb ddb
Answer

UPDATE: EDIT 3 is the working solution


you should add an ng-if like this

ng-repeat="employeeInfo in employees" ng-if="employeeInfo.id!=currentEmployee.id"

(I suppose you have an angular variable currentEmployee that you can use for such a check)

EDIT

try to add this ng-if after the ng-options of the drop down select

ng-options="supervisor.id as (supervisor.name + ' ' + supervisor.lastname + ' ' + supervisor.id) for supervisor in supervisorList" ng-if="employeeInfo.id!=supervisor.id"

EDIT 2

before executing the ng-options that generates the drop down, execute this piece of code

supervisorListCopy = $filter('filter')(supervisorList, function(value, index) {return value.id !== employeeInfo.id;})

then you can generate the drop down using the filtered list

ng-options="supervisor.id as (supervisor.name + ' ' + supervisor.lastname + ' ' + supervisor.id) for supervisor in supervisorListCopy"

EDIT 3

the simplest way to do it is to apply filter aside the ng-options like below

ng-options="supervisor.id as (supervisor.name + ' ' + supervisor.lastname + ' ' + supervisor.id) for supervisor in supervisorListCopy | filter: '!' + employeeInfo.id"