Search code examples
javascriptangularjsdrop-down-menufilterng-options

angularjs filter drop-down list according to selected values in other drop-down lists


I want to implement the following functionality:

Suppose you have 1 input field and 2 drop-down lists. At the input field you can fill in your e-mail address and next to that you can choose what kind of type this e-mail is (Personal, Professional, Other, or nothing).

Now in the third drop-down list you will see a list of e-mails where you can choose from, the e-mail address you prefer.

So what should happen:

1) If there is nothing in the input field the preferred e-mail drop-down list is empty (this is already the case).

2) When there is an e-mail filled in AND a TYPE, the preferred e-mail drop-down list should contain this value: test@test.com (Personal) for example.

3) When there is an e-mail filled in but no TYPE, the preferred e-mail drop-down list should contain this value: test@test.com for example so without the type.


HTML:

<div ng-repeat="email in contactInfo.emails">
    <input id="email" type="text" ng-model="email.email"/>
    <select id="emailType" ng-model="email.emailTypeId" ng-options="emailType.id as emailType.name for emailType in emailTypes">
        <option value="">Choose...</option>
    </select>
</div>

<br/><br/>

<label>Preferred e-mail:</label>
<select style="margin-left: 20px; width: 50%;" id="preferred-email" ng-model="contactInfo.preferredEmail" ng-options="email.email for email in (contactInfo.emails | filter:filterEmail) track by email.id">
    <option value="">Choose...</option>
</select>


JAVASCRIPT:

function MyCtrl($scope){
    $scope.contactInfo = {};
    $scope.emailTypes = [{"label":"Personal","id":1,"name":"Personal","rank":2},{"label":"Professional","id":2,"name":"Professional","rank":2},{"label":"Other","id":3,"name":"Other","rank":4}];

    $scope.contactInfo.emails = [{"id":1100, "emailTypeId":2,"email":"member@test.com"}, {"id":1200, "emailTypeId":1,"email":"member2@test.com"}];
    $scope.contactInfo.prefferedEmail = {};

    $scope.filterEmail = function(email){
        return (email.email);
    }
}


JSFIDDLE:

HERE is the fiddle but only the first one is working.

I don't have ant clue so it would be great if someone could help me with this. Thank you for your time.

Sven.


Solution

  • Here is a sample implementation - http://jsfiddle.net/iamgururaj/T7fkH/5/

    Code:

    <select style="margin-left: 20px; width: 50%;" id="preferred-email" ng-model="contactInfo.preferredEmail" ng-options="getEmail(email) for email in (contactInfo.emails | filter:filterEmail) track by email.id">
        <option value="">Choose...</option>
    </select>
    

    JS:

    $scope.contactInfo = {
            emails: [{
                "id": 1100,
                    "emailTypeId": "2",
                    "email": "1@test.com"
            }, {
                "id": 1200,
                    "emailTypeId": "1",
                    "email": "2@test.com"
            }]
        };
        $scope.emailTypes = {
            "1": "Personal",
                "2": "Professional",
                "3": "Other"
        };
        $scope.filterEmail = function (email) {
            return (email.email);
        }
    
        $scope.getEmail = function (email) {
            if (email.emailTypeId) {
                return email.email + ' (' + $scope.emailTypes[email.emailTypeId] + ')';
            }
            return email.email;
        }