0

In my edit form i am not able to retrieve the values which are stored at the addition time. The flow is like when an option of the first select box is selected after that it's relevant data will be displayed in the next select box.

I have tried this answer for selected value but the values are not displaying.

I have a form as below:

<form [formGroup]="productForm" (ngSubmit)="onSubmit()">
    <mat-form-field">
      <mat-label>Main Type</mat-label>
      <mat-select [(value)]="selectedType" (selectionChange)="onTypeChange($event.value)"
        formControlName="mainTypeId">
        <mat-option *ngFor="let list of mainList" [value]="list.id">{{list.name}}</mat-option>
      </mat-select>
    </mat-form-field>
    <mat-form-field">
      <mat-label>Sides Type</mat-label>
      <mat-select [(value)]="selectedSide" formControlName="sideTypeId">
        <mat-option *ngFor="let list of sidesList" [value]="list.id">{{list.name}}
        </mat-option>
      </mat-select>
    </mat-form-field>
</form>

EditProductComponent.ts

export class EditProductUploadImageComponent implements OnInit {
    public selectedType: any;
  public selectedSide: any;

  constructor(private fb: FormBuilder,
    private productService: ProductService) {
    this.formInitialization();
    this.getSelectedData()
  }

    getSelectedData() {
    this.productService.getProductDesignRef(this.data.editId).subscribe((response: any) => {
        this.refrences = response.data[0]
        console.log(this.refrences)
        this.productService.getMainListById(this.refrences.main_type_id).subscribe((response: any) => {
          this.selectedType = response.data[0].id
          this.getMainTypeList()
          if(response) {
            this.productService.getSidesListById(this.refrences.sides_type_id).subscribe((response: any) => {
              this.selectedSide = response.data[0].id
              this.onTypeChange(1)
            })
          }
        })
    })
  }

  getMainTypeList() {
    this.productService.getMainTypeList().subscribe((response: any) => {
      if (response) {
        this.mainList = response.data;
        console.log(this.mainList)
      }
    }
  }

  onTypeChange(value: number) {
    this.productService.getSidesTypeListById(value).subscribe((response: any) => {
      if (response) {
        this.mainTypeListById = response['data']['0']['sides-type'];
        this.sidesList = this.mainTypeListById
        console.log(this.sidesList)
      }
    }
  }
}

=> By using the reference ids i have passed a selected value in the form. But can not able to get proper output.

Pls review all console logs in this jsfiddle link

For more clarification pls review these two images regarding work flow.

  1. Listing of the records: https://prnt.sc/rw2ucu ; while clicking on the edit button an edit window will be open and there i want that two listed values which are in the listing page for updating the records.
  2. Edit dialog box: https://prnt.sc/rw2xeh

Thanks in advance...!!!

TMA
  • 1,419
  • 2
  • 22
  • 49

2 Answers2

2

I just noticed that you are using reactive forms.

<!-- why are you using value and formControlName & why you passing $event.value in your method? -->
<mat-select 
   [(value)]="selectedType" 
   (selectionChange)="onTypeChange($event.value)"
   formControlName="mainTypeId"
>

<!-- when you are using reactive forms you can just access mainTypeId in your onTypeChange method -->
<mat-select 
   (selectionChange)="onTypeChange()"
   formControlName="mainTypeId"
>

// your method
onTypeChange() {
   // your access mainTypeId here directly like this
   const mainTypeId = this.yourFormName.get('mainTypeId').value;
}

You can easily handle your edit case my using [compareWith] if the above doesn't work for your edit case.

AND IF YOU WERE NOT USING REACTIVE FORM:

  1. First thing is that I can not see any use of value in your onTypeChange method.
  2. In here (selectionChange)="onTypeChange($event.value)" you don't have to pass $event.value because for the value you are already setting in selectedType
  3. You just need to access in this.selectedType in your onTypeChange method

If it's unclear please comment I'll get back to you.

Immad Hamid
  • 789
  • 1
  • 8
  • 21
  • Not working sir... i wonder why no any solution is working. I have created this on https://stackblitz.com/edit/angular-p9ruf4 ; might be you can get better idea about the flow. – TMA Apr 09 '20 at 11:21
  • That's great, I'll get back to you in 30 mins or less – Immad Hamid Apr 09 '20 at 11:26
  • Thank you sir. For more clarification pls review these two images regarding work flow. 1) Listing of the records: https://prnt.sc/rw2ucu ; while clicking on the edit button an edit window will be open and there i want that two listed values which are in the listing page for updating the records. 2) Edit dialog box: https://prnt.sc/rw2xeh – TMA Apr 09 '20 at 11:59
  • The stackblitz link that you have shared. The application seems to be broken. You should have setup minimal code structure. I can do a zoom call with you in order to resolve your issue, you can share your screen and I can help find the issue. – Immad Hamid Apr 09 '20 at 12:07
  • It's working now sir as your answer above...!!! I checked the steps again and resolve a change in the object key name. Thanks a ton. Cheers. – TMA Apr 09 '20 at 12:25
  • Your are welcome... I would have scheduled a call if you still had any issue. It's great that you did it on your own. – Immad Hamid Apr 09 '20 at 12:30
  • Hello sir, It would be great help if you help me out on this question. https://stackoverflow.com/q/61341301/7995957 P.S I am sorry about asking help in different thread, but can not get the other way to reach out. – TMA Apr 22 '20 at 09:26
0

You cannot use [(value)] and formControlName on same material component. It do not behave.

And update this.selectedType = response.data[0].name to this.selectedType = response.data[0].id in your getSelectedData function

You are not using selected value in function onTypeChange

onTypeChange(value: number) {
  this.productService.getSidesTypeListById(1).subscribe((response: any) => {
     if (response) {
       this.mainTypeListById = response['data']['0']['sides-type'];
       this.sidesList = this.mainTypeListById
       console.log(this.sidesList)
     }
   }
}

You have to use value to see impact.

Gourav Garg
  • 2,856
  • 11
  • 24
  • In my actual code i have already passed a *value* parameter and it is impacting when i click on the first selection... But here my requirement is when i load an edit form at that time the added value should be retrieve. – TMA Apr 09 '20 at 10:24
  • Are you setting this "selectedType" somewhere? – Gourav Garg Apr 09 '20 at 10:28
  • you are assigning string and in HTML you are using id as value. – Gourav Garg Apr 09 '20 at 10:32
  • 1
    change this this.selectedType = response.data[0].name to this.selectedType = response.data[0].id or you can create a stackblitz – Gourav Garg Apr 09 '20 at 10:41
  • Hello sir, here is a stackblitz link: https://stackblitz.com/edit/angular-p9ruf4 P.S: I did not get about how to update api calls in stackblitz and also removed the older comments so that the thread can not be exceed. – TMA Apr 09 '20 at 11:19