Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Angular material table et dialog CRUD

I am making a CRUD method using angular material table and dialog, I don't understand how to make the update method using the dialog how to pass the data from one component to another.

My modify line doesn't update how to do it and what's wrong with my code ?

thanks.

service

  getTableDetails(): Observable<any[]> {
    return this.http.get<any[]>(this.url);
  }

  create(name: any): Observable<any> {
    return this.http.post<any>(this.url, name);
  }

  update(id: any, data: any): Observable<any> {
    return this.http.put<any>(this.url + '/' + id, data);
  }

  delete(id: any): Observable<number> {
    return this.http.delete<any>(this.url + '/' + id);
  }

ts.file

  openDialog(element: any) {
    const dialogRef = this.dialog.open(EditTableDialogComponent, {
      data: element,
    });

    dialogRef.afterClosed().subscribe(result => {
      console.log(`Dialog result: ${result}`);
    });
  }

dialog.html

<div class="inputAdd">
  <h2>Edit Table</h2>
  <input #input value="{{element.name}}" required>
  <button (click)="edit(input.value)">Modifier</button>
</div>

dialog.ts

export class EditDialogComponent {
  constructor(
    @Inject(MAT_DIALOG_DATA) public data: MatDialog,
    private tableService: TableService
  ) {}

  public element: any = this.data;

  edit(value: any) {
    this.tableService.update(this.element.id, value).subscribe((data:any) => {
      console.log(data);
    });
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I believe you should not perform the update, or the creation of the item inside the dialog component. That should not be concerned with such a responsibility, otherwise it will get harder for you to maintain it. It should only allow the user to make changes to the entity and that's it. Whenever you close the modal, you can subscribe to the afterClosed event and decide there what you should do with the entity. Usually, if the entity has an id, it means that you need to update an existing entry, otherwise create a new one.

The dialog component should also inject the dialogRef, so that you can close the dialog and also provide a result:

export class EditDialogComponent {
  constructor(
    @Inject(MAT_DIALOG_DATA) public data: MatDialog,
    private tableService: TableService,
    private dialogRef: MatDialogRef<EditDialogComponent>
  ) {}

  public element: any = this.data;

  edit(value: any) {
    this.dialogRef.close(this.element);
  }
}

In the component that renders the table, you should open the modal like you already do:

openDialog(element: any) {
  const dialogRef = this.dialog.open(EditTableDialogComponent, {
    data: element,
  });
  dialogRef
    .afterClosed()
    .pipe(
      filter((element) => !!element), // in case the user closes the modal without pressing the "Modifier" button
      switchMap(
        (element) => iif(() => element.id),
        this.tableService.update(element.id, element),
        this.tableService.create(element)
      )
    )
    .subscribe((data) => {
      console.log('success', data);
    });
}

And also decide what to do with the item (create or update).

about 4 years ago · Juan Pablo Isaza Relatório

0

In you component try use Formbuilder and input.getValue()

<div [formGroup]="formInput" class="inputAdd">
  <h2>Edit Table</h2>
  <input #input value="{{element.name}}" formControlName="myInput">
  <button (click)="edit(input.value)">Modifier</button>
</div>

in TS

constructor(private formBuilder: FormBuilder) { }

createForm() {
    this.formInput = this.formBuilder.group({
      myInput: [cliente.nome],
    })
  }

edit(value: any) {
    sendValue = this.createForm.myInput.getValue;
    this.tableService.update(this.element.id, sandValue).subscribe((data:any) => {
      console.log(data);
    });
  }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda