In my angular application, I have a form Form Page in which employee data can be entered and I have a grid page Grid Page where the entered data will be displayed. When I submit the form, the details should get added in the grid page.
Initially, I'll fetch the employees' data in grid from json file.
The issue I face here is, even after submitting the form, the new data is not getting added to the grid. Grid is always loading from json file. The pushed data from the form should get added to the grid which is not happening.
In my service, I have the following code:
import { Employee } from '../employee.model';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Subject } from 'rxjs/Subject';
//import 'rxjs/add/operator/map';
@Injectable()
export class BasicServices {
public newaddFormDetails = new Subject<Employee[]>();
private _url: string = "/assets/data/employeesData.json";
public employees: Employee[];
// private employees: Employee [] =[ {"title":"John", "body":"TL"},
// {"title":"Kennedy", "body":"SSE"}];
// getEmployees() {
// return this.employees;
// }
getEmployees() {
return this.httpClient.get<Employee[]>(this._url)
.subscribe(
(employees:Employee[]) => {
this.employees = employees;
this.newaddFormDetails.next(this.employees);
});
}
addToGrid(emp: Employee) {
this.employees.push(emp);
this.newaddFormDetails.next(this.employees);
}
constructor(private httpClient: HttpClient) {
}
}
The grid component file is below:
import { Component, OnInit, OnDestroy} from '@angular/core';
import { BasicServices } from '../services/basic.services';
import { Subscription } from 'rxjs/Subscription';
import { Employee } from '../employee.model';
@Component({
selector: 'app-grid',
templateUrl: './grid.component.html',
styleUrls: ['./grid.component.less']
})
export class GridComponent implements OnInit, OnDestroy{
subscription: Subscription;
employees: Employee[];
constructor( private _basicService: BasicServices) {
}
ngOnInit() {
this.subscription = this._basicService.newaddFormDetails.subscribe(
(employees: Employee[]) => {
this.employees = employees;
}
);
this._basicService.getEmployees();
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
In my grid component html file, I simply have a table with the code below:
<table class='table table-striped table-condensed' *ngIf='employees && employees.length'>
<thead>
<tr>
<th style="min-width: 40px;">Title</th>
<th style="min-width: 40px;">Body</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let emp of employees">
<td>{{emp.title| uppercase}}</td>
<td>{{emp.body}}</td>
<td>
</tr>
</tbody>
</table>
Form component file:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { BasicServices } from '../services/basic.services';
import { Router } from '@angular/router';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.less']
})
export class FormComponent implements OnInit {
private form;
constructor(private _basicService: BasicServices,
private formBuilder: FormBuilder,
private router: Router
) { }
ngOnInit() {
this._basicService.getEmployees();
this.form = this.formBuilder.group({
title: [null, Validators.required],
body: [null, Validators.required],
});
}
onSubmit(formValue):void {
this._basicService.addToGrid(formValue);
this.form.reset();
this.router.navigate(['/grid']);
}
}
Form component template
<div class="container">
<h1> Add Records </h1>
<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)">
<div class="form-group">
<label> Title : </label>
<input type="text" class="form-control" required formControlName="title" placeholder="FirstName" [(ngModel)] ="title">
</div>
<div class="form-group">
<label> Body : </label>
<input type="text" class="form-control" required formControlName="body" placeholder="LastName">
</div>
<input type="submit" class="btn btn-info" name="Submit" [disabled]="!form.valid">
</form>
</div>