1

I have data in the grid api, ag-grid. When I select a row and click the edit button, I would like for that row's data to be loaded into the correct form fields sot Example, But I am confused how to how properly pass my data to the mat dialog. Here is my example. First I have a service that makes a put request and another service that posts the data:

editUser(employee: Employees) {
  return this.http.put(this._url + '/' + employee.id, httpOptions).pipe(
    map((res: Response) => res.json)
  )
}

saveUser(employee: Employees) {
  return this.http.post(this._url, employee, httpOptions).subscribe((res: any) => {
    res = employee
    this.componentMethodCallSource.next();
  })
}

Then in my table.component class, I have the function for my button. This selects the row, calls the service, and opens the dialog.

editUser() {
  const selectRows = this.gridApi.getSelectedRows();

  const editSub = selectRows.map((rowToEdit) => {
    return this.service.editUser(rowToEdit);
  });
  this.openDialog(FormComponent)
}

Here is where I am stuck, in my form class, I need to set a condition in my ngoninit. This condition determines if I call my loadUser function. Here, I try and use MAT_DIALOG_DATA to load the json id which would determine if loadUsers() should be called. But I cannot seem to access my id. I keep getting an error saying it is undefined.

Form Component

employeeInfo: Employees;
userId: number;

constructor(
  public dialogRef: MatDialogRef<FormComponent>,
  private fb: FormBuilder, 
  private service: Service, 
  private http: HttpClient, 
  @Inject(MAT_DIALOG_DATA) public data: any
) {
  this.userId = this.data.userId;
}

ngOnInit() {
  this.createUserForm();
  if (this.userId > 0) {
    this.loadUser();
  }
}

createUserForm() {
  this.frmUser = this.fb.group({
    id: [],
    firstName: ["", [Validators.required]],
    lastName: ["", Validators.required],
  })
}

onSave() {
  this.employeeInfo = this.frmUser.getRawValue();
  this.employeeInfo.id = this.userId;
  this.service.saveUser(this.employeeInfo)
  this.dialogRef.close({
    success: 'success',
    data: {
      emp: this.employeeInfo.id
    }
  })
}

loadUser() {
  this.service.getData(this._url).subscribe((res: any) => {
    this.frmUser.patchValue({
      firstName: res.FirstName,
    })
  })
}

I am using the MatDialogRef for my form.

EDIT: Updated code. Unable to send data to the form still: TableComponent:

editUser(user) {
  const dialogRef = this.dialog.open(FormComponent, {

    width: '250px',
    data: user
  });

  dialogRef.afterClosed().subscribe(result => {
    this.user = user;
  });
}

Form Component:

  constructor( public dialogRef: MatDialogRef<FormComponent>,
    private fb: FormBuilder, private service: Service, private http: HttpClient,  @Inject(MAT_DIALOG_DATA) public data: Employees) { 
    }
John
  • 481
  • 1
  • 6
  • 13
  • Why are you making the `put` call? Isn't that a bit redundant? Shouldn't it either be `put` or `post`? – SiddAjmera Aug 21 '18 at 16:23
  • I use a put request so that I can overwrite the existing data from my server. But I am confused on how to implement this since after editing a user, I am calling the same save service. So maybe I should not use the put request, and just use a get request. The problem is displaying the requested data in the form. – John Aug 21 '18 at 17:22
  • where exactly are you opening mat-dialog? That's where you'll be passing the user data. – SiddAjmera Aug 21 '18 at 17:37

1 Answers1

1

I suggest you read the documentation of Material Dialog. I'm also not exactly sure why you're creating a form using the formBuilder if you want to show the user edit form on the dialog.

You pass the data to the open method of MatDialog's instance.

constructor(public dialog: MatDialog) {}

this.dialog.open(DialogOverviewExampleDialog, {
  width: '250px',
  data: user
});

user is the variable that contains the data that you'll be passing to the modal.

I've created a StackBlitz Project replicating your case. Let me know if this is what you're looking for.

I've taken the data from JSONPlaceholder API and I'm simply reflecting the changes that the user does to the Material Table instead of the AgGrid Table as in your case.

But your main issue was to get the userId in the form component. So that's something that has been replicated in the project. And that should help you understand what the issue is at your end.

NOTE: Make sure to use the following versions to avoid any version mismatch errors:

@angular/material - 6.4.6 @angular/animations - 6.0.5

Do have a look at the Dependencies Section of the Project for more info on the versions of packages installed.

SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
  • Thank you for all the help. Your example has set me in the right direction. I just need to figure out how to apply the logic from your example to selecting a row in the grid, then clicking a delete button. Thanks again. – John Aug 21 '18 at 18:07
  • Glad I helped. :) – SiddAjmera Aug 21 '18 at 18:07
  • Hmmm, so I applied buttons to my grid and tested that they work. But I still cannot seem to get my data from the row to display in my form. I have followed many examples simular to yours and am a bit puzzled. I belive it may have something to do with the grid. If you would like to take a look, I've updated my question. – John Aug 21 '18 at 20:08
  • This may provide some insight... In the editUser function if I `console.log(user.id)`, I get `undefined`. But if I `console.log(user.rowData.id)`, I would get the desired id. – John Aug 21 '18 at 21:00