1

I am sure this is a simple answer but I just cannot seem to solve it.

I have a dropdown list with names in it John, Jack, Jill, etc. I have a second List with professions in Builder, Joiner, Plasterer, etc.

What I am trying to do is when I select John, I know based on his data he is a Builder, so in my second list I want to change the selected value from "select" to "Builder".

Below is a plunker with what I currently have, any advise on how to make this would would be great.

enter link description here

This is my change method for my first dropdown but I am not getting the desired result:

     firstDropDownChanged(val: any) {
    console.log(val);

    if (val.profession == "Builder") {
      this._values2.selected = "Builder";
    }
    else if (val.profession == "Plumber") {
      this._values2.selected = "Plumber";
    }
    else if (val.profession == "Plasterer") {
      this._values2.selected ="Plasterer";
    }
   else if (val.profession == "Joiner") {
      this._values2.selected ="Joiner";
    }

Thanks Andy

stephenad
  • 55
  • 1
  • 12

2 Answers2

1

This is what I was able to get to work based on the code in the Plunk.

import {Component, NgModule } from '@angular/core'
import { FormsModule } from '@angular/forms';
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>

      <select (change)="firstDropDownChanged($event.target.value)" >
        <option>Select</option>
        <option *ngFor='let v of _values1'>{{ v.name }}</option>
      </select>

      <select [(ngModel)]="selectedProfession" >
        <option>Select</option>
        <option *ngFor='let v of _values2'>{{ v }}</option>
      </select>
    </div>
  `,
})
export class App {
  name:string;
  selectedProfession:string;

  private _values1 = 
  [
    {name:"John", profession: "Builder"},
    {name:"Jack", profession: "Builder"},
    {name:"Jeff", profession: "Plumber"},
    {name:"Jill", profession: "Plasterer"},
    {name:"Joan", profession: "Joiner"},
  ];
  private _values2 = ["Builder","Plumber"," Joiner", "Plasterer"];

  constructor() {
    this.name = 'Angular2'
  }

  firstDropDownChanged(val: any) {
    console.log(val);
    this.selectedProfession = this._values1.find(item => item.name === val).profession;
  }
}

@NgModule({
  imports: [ BrowserModule, FormsModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

I added the FormsModule so I could bind the select value to a property (selectedProfession) in the component.

Then I changed the firstDropDownChanged function to find the choosen worker in the _values1 array based on name, and set the value of the dropdown to that person's profession.

R. Richards
  • 24,603
  • 10
  • 64
  • 64
0

If you don't actually need the variable selectedProfession easiest way would be to in the first dropdown we have the complete object, so that we can use it in the second dropdown. This way we need no methods. So the first one would look like this:

<select [(ngModel)]="tradesman">
  <option>Select</option>
  <option [ngValue]="v" *ngFor='let v of _tradesmen'>{{ v.name }}</option>
</select>

where we use [ngValue] to bind the whole object, the second one would then look like:

<select [(ngModel)]="tradesman.qualifications">
  <option>Select</option>
  <option *ngFor='let v of _qualifications'>{{ v.qualifications }}</option>
</select>

This way is also good if you want to capture any possible change in the qualifications, the tradesman variable will have the change.

Here's demo: http://plnkr.co/edit/TFXUtezSuJsJVuHNXfQ3?p=preview

AT82
  • 71,416
  • 24
  • 140
  • 167
  • Hi all thanks for the advice and help really appreciated, one last thing I am trying to do and have update my plunker to have the update is I now have set 3 dropdowns, the first, is the tradesmen's name like before, the second I have renamed to company and like before it now shows the company based the name selected, the third box is now going to be the qualifications. Where I need it to show all the qualifications the trades man has, I have implemented your setup and if they have 1 it shows the qualification, but if it has more it does not show anything please advise. – stephenad Oct 08 '17 at 18:27
  • Hi All I have updated my Plunker further I have currently got 2 issues with 2 way databinding: 1) I need to populate a multiselect box based on the trades mans qualifications? but it is not working not sure what I am overlooking: 2) Trades man have roles, ie General or Admin or both, I need to do and if statement within my ngModel object to check if the string has admin and mark the admin check box please can someone advise https://plnkr.co/edit/z2RCRX3KEw02FLhVxXKe?p=preview Thanks a million Andy. – stephenad Oct 09 '17 at 11:35
  • I was feeling kind today so I'm helping out :P Please always just ask one question and if you have followup-questions, post a new question. Since actually your question was answered before all your new questions. But here is the plunker to solve your issues: http://plnkr.co/edit/HXXHEYVXnAG3bIyvsLMS?p=preview Now when everything is solved, please consider accepting an answer by clicking the grey tick under the voting of answer. Check this one for more info about accepting: https://meta.stackexchange.com/a/5235 – AT82 Oct 09 '17 at 13:29
  • Hi AJT, Thank you so much, I did actually post a new question as well as I was not sure if my post here would have been seen. I just kept it on here as it was related / connected so thought it would be better to keep the full picture. Thanks Again Andy – stephenad Oct 09 '17 at 13:58
  • Hi AJT, I have found 1 issue my db is returing a string of qualifications not an array how would I change the code to handle the string instead of the array. Do I do it the same as you did the user roles boxes? Should this have been a new question? – stephenad Oct 09 '17 at 16:05
  • How does the string look like exactly when you receive it? – AT82 Oct 10 '17 at 04:15
  • Hi AJT, I looks like this: qualifications: "Joiner, Carpenter, CabinetMaker" Thanks – stephenad Oct 10 '17 at 07:54
  • http://plnkr.co/edit/HXXHEYVXnAG3bIyvsLMS?p=preview Please, now if you have further questions, please ask a new question. I suspect most people would have asked you to do that before, since your original question has been answered long time ago ;) – AT82 Oct 10 '17 at 08:47
  • You are welcome, please consider accepting an answer, since it solved your issue. Furthermore, have a nice day and happy coding! – AT82 Oct 10 '17 at 09:46