0

I'm trying to edit/update a form from another view by elementId.

employee-list.comonent.ts(view 2):

 onEdit(empId:string){
this._router.navigate(['/RegisterEmployee',empId]);
//this.service.formData=Object.assign({},emp);
 }

on click it navigates to another view with Id of selected element but How can i populate form-data from database of selected element and update the edited data on firebase database

employee.component.ts(view 1)-Reactive-Form

 ngOnInit() {
//this.resetForm();
this.form = this.formBuilder.group({
  fullName:new FormControl('',[Validators.required,Validators.pattern('[a-zA-Z][a-zA-Z ]+')]),
  designation: new FormControl('',[Validators.required,Validators.pattern('[a-zA-Z][a-zA-Z ]+')]),
  Email: new FormControl('',[Validators.required, Validators.email]),
  mobile: new FormControl('',[Validators.required, Validators.pattern('[0-9]*'),Validators.minLength(10),Validators.maxLength(10)])
});
 }

 /* submit the data to be added into database having 'employees' as the data 
 collection array*/
 onSubmit():void{
console.log(this.form.value);
this.router.navigateByUrl('/EmployeeList');
let data = Object.assign({},this.form.value);
if(this.form.value.id == null)
this.firestore.collection('employees').add(data);
else
this.firestore.doc('employees/'+this.form.value.id).update(data);
//this.resetForm(this.form);
this.toastr.success('Submitted successfully','Employee Register');
 }

And my service for the same as below: employee.service.ts

import { Injectable } from '@angular/core';
import { Employee } from './employee.model';
import { AngularFirestore } from '@angular/fire/firestore';
@Injectable({
providedIn: 'root'
})
export class EmployeeService {
formData : Employee;

constructor(private firestore:AngularFirestore) { }

/*Function to get added employees list from firebase database*/
getEmployees(){
let listOfEmployees = 
 this.firestore.collection('employees').snapshotChanges();
 return listOfEmployees;
  }
   }

my interface for the form : employee.model.ts

export class Employee {
id : string;
fullName: string;
Email: string;
mobile: string;
designation :string;
}

please Help with this(new to angular and my first project using firebase).

here is the git repository for working application https://github.com/HusnaKhanam/MyApplication

1 Answer 1

2

If I understand correctly, here are the basic steps to populate the form with data:

In the employee component:

1) Read the id from the url parameter similar to this:

  ngOnInit() {
    const param = this.route.snapshot.paramMap.get('id');
    if (param) {
      const id = +param;
      this.getProduct(id);
    }
  }

(NOTE: This is my code, you'll need to modify it for your specific scenario.)

2) Get the data from the service similar to this:

  getProduct(id: number): void {
    this.productService.getProduct(id)
      .subscribe(
        (product: Product) => this.displayProduct(product),
        (error: any) => this.errorMessage = <any>error
      );
  }

3) Update the data on the form using code similar to this:

  displayProduct(product: Product): void {
    this.product = product;

    // Update the data on the form
    this.productForm.patchValue({
      productName: this.product.productName,
      productCode: this.product.productCode,
      starRating: this.product.starRating,
      description: this.product.description
    });
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.