Home > OS >  how to push afterclosed in mat dialog Angular
how to push afterclosed in mat dialog Angular

Time:09-07

I want to Iterate my formarray depending on what qty data i have for example: my serial number is 3 so it will push my array in to 3 rows. how to achieved that? so far this is what i code.

https://stackblitz.com/edit/mat-dialog-example-qjwsd5?

CodePudding user response:

So your question is not clear, I am happy for the stackblitz, but I need screenshots of how you want the output to be, anyway this is my version from what I understood, hope it helps!

ts

import { Component, Inject } from '@angular/core';
import {
  VERSION,
  MatDialogRef,
  MatDialog,
  MatSnackBar,
  MAT_DIALOG_DATA,
} from '@angular/material';
import { ConfirmationDialog } from './confirmation-dialog.component';
import { AlertDialogComponent } from './alert-dialog/alert-dialog.component';
import { FormGroup } from '@angular/forms';
import { FormBuilder } from '@angular/forms';
import { OnInit } from '@angular/core/src/metadata';
import { FormArray } from '@angular/forms';
import { async } from '@angular/core/testing/src/async';
@Component({
  selector: 'material-app',
  templateUrl: 'app.component.html',
})
export class AppComponent implements OnInit {
  version = VERSION;
  value = '';
  fg: FormGroup;
  showButton = true;

  constructor(
    private dialog: MatDialog,
    private snackBar: MatSnackBar,
    private fb: FormBuilder
  ) {
    this.fg = this.fb.group({
      name: [],
      lastname: [],
      Info: this.fb.array([this.item()]),
    });
  }

  ngOnInit() {
    this.item();
  }
  item(): FormGroup {
    return this.fb.group({
      qty: [],
      total: [],
    });
  }
  get Info(): FormArray {
    return this.fg.get('Info') as FormArray;
  }

  addRow() {
    return this.Info.push(this.item());
  }
  openAlertDialog(index: number) {
    const dialogRef = this.dialog.open(AlertDialogComponent, {});
    dialogRef.afterClosed().subscribe((result) => {
      for (let i = 0; i < result.qty; i  ) {
        this.Info.push(this.item());
      }
      for (let i = 0; i <= result.qty; i  ) {
        this.Info.at(i).patchValue({ qty: result.qty });
      }
    });
  }
  OpenSemame() {
    const dialogRef = this.dialog.open(AlertDialogComponent, {});
  }
}

forked stackblitz

  • Related