How to make cascading dropdown in a Angular Reactive Formarray work without messing the dropdown value
Asked Answered
G

1

12

I have a form in angular 4 that containing First Name + Last Name and a formarray containing 2 dropdown(select) working as cascading dropdown and a delete button. The rest of the form also contains a send button and a add option button. I added a screenshot down here for you to understand better. The forms add , remove button and send button works there is 1 problem the cascading dropdown works only when there is 1 cascading dropdown ,when I add an extra cascading select the value from previous groups of cascading select second select get messed up. I added pics down here for a better explantion

enter image description here

As you can see on the 2nd and the 3rd picture the cascadingdropdown is working When I change select in Criteria I fet the right options to select in the 2nd dropdown enter image description here

enter image description here

enter image description here

On the 4th picture 5th Ppicture and 6th Picture you can see the add options button works but when I select a Criteria in Option 2 first dropdown it messed up with Option 1 2nd Dropwown also now it contains the dropdown choices from the 2nd Option

enter image description here

enter image description here

Here is my html code

<form [formGroup] = "profileForm">

  <h1>Profile Form</h1>

 <div>
  <label for = "first-name-input">First Name</label>
  <input type="text" id="first-name-input" formControlName ="firstNameInput">
</div>
 <div>
    <label for = "last-name-input">Last Name</label>
    <input type="text" id="last-name-input" formControlName ="lastNameInput">
  </div>

  <div formArrayName="optionGroups">

    <div *ngFor="let optionGroup of profileForm.controls['optionGroups'].controls;  let i=index "[formGroup]="optionGroup">


      <h4>Option {{ i + 1 }} </h4>




     <div>
      <label for = "select-input">Criteria</label>
      <select id="select-input" (change)="onSelectSelect($event.target.value, i)"  formControlName ="selectInput">

          <option value="0" disabled selected>Select a Criteria</option>
          <option *ngFor="let select of selects"  [value]= "select.name">{{select.id}}</option>


        </select>

        <label for = "where-input">Option</label>
        <select id="where-input" formControlName ="whereInput">
  
            <option value="0" disabled selected>Select a Option</option>
            <option *ngFor="let where of wheres" value= {{where.name}}>{{where.id}}</option>


          </select>

          <button type ="button" (click)="removeOptionGroup(i)">X</button>

         


     </div>

    </div>

   </div>
   <button type ="button" (click)="addOptionGroup()">Add Options</button>
   <div>

    <button type ="button" (click)="saveProfileForm()">Send </button>

   

   </div>

</form>

And here is my component file

export class PowComponent {

 selects: Select[];
 wheres: Where[];


 public profileForm : FormGroup;

 public addOptionGroup(){

 const fa = this.profileForm.controls["optionGroups"] as FormArray;

  fa.push(this.newOptionGroup());

  }

  public removeOptionGroup(index: number){

   const fa = this.profileForm.controls["optionGroups"] as FormArray;

    fa.removeAt(index);

   }


   public saveProfileForm(){

    console.log(this.profileForm.value);
   }



   constructor(public router: Router,  public dropdownqueryservice:    DropDownQueryService , private fb : FormBuilder) {
    this.profileForm = this.fb.group({

        firstNameInput : [ "" ],
        lastNameInput : [ "" ],
        optionGroups : this.fb.array([
            this.fb.group({

              selectInput : [ "" ],
              whereInput : [ "" ],

          }),
       ]),
    });

    this.selects = this.dropdownqueryservice.getSelect();
    this.wheres=[];

}

onSelectSelect(selectid, i) {
    this.wheres = this.dropdownqueryservice.getWhere().filter((item)=> item.selectid == selectid);

  }


 private newOptionGroup() {
  return new FormGroup({

      selectInput: new FormControl(""),
      whereInput: new FormControl(""),

  });
   }

  }
Goldsberry answered 8/4, 2018 at 20:17 Comment(3)
try using ngValue instead of value, see: #33700766Cerracchio
Im trying with ngValue but was getting more errors .Goldsberry
You are facing this issue because you have only one selects parameter and two different selects values. You have to handle multiple selects arrays with possible select values for each multiple choices input.Blackfish
B
7

As I mentionned previously on the comment, the issue is comming from the unique selects array for multiple select forms.

You will have to create an array of arrays of Select, that contains an array of possible criteria for each select value.

Here is a stackblitz solution suggestion, I did some modification to keep it simple and to make it work. Feel free to fork and edit if you want to get more explicit about your concern.

Blackfish answered 13/4, 2018 at 12:34 Comment(6)
hi I am very busy I have seen your code but since my code has an array it I may have to figure out how to do that ,since the bounty is expiring I am giving to you. later today or tomorrow I will fork your code and post it here I am half way through it. thxGoldsberry
I tried your code but I think since I am using an array of objects its different here is my code stackblitz.com/edit/stackoverflow-49722349-6pvgkhGoldsberry
Here is a fixed version of your stackblitz with the suggested solution : stackblitz.com/edit/stackoverflow-49722349-2Blackfish
Thanks for the sample solution! One issue that I have with it is that when you change the first drop down - the second drop down keep the old value, which is no longer valid. What is the best way to clear the second drop box also? In my case - I use cascading drop downs which progressively filter down. So - if old value is still valid - keep it, if it isn't - clear it.Lilt
@NickGoloborodko That's one interesting remark, didn't notice. I don't know yet how it could be handled properly, could you please create a new question for this issue ?Blackfish
@ibenjelloun, Hi pretty new on Angular and I am working around your solution. Which is fantastic but I am wondering there is any way to add a file upload option. Say when the user selects 'Location' from the drop-down menu, it will show file upload rather than another dropdown menu! Also, I would like to show the file content in output!Victorious

© 2022 - 2024 — McMap. All rights reserved.