Reputation: 59
project with 4 components 1. Login 2. Home(to view minute detail of Employee) 3. View(to show complete Details of Employee) and 4. Edit. Am already able to read Employee Details from .json file and display on the View in Detail component and View Component.
Now I want to change the details available in JSON file and display the same on View and ViewinDetail components.
My Employee.json is as follows:
[
{
"id": 1,
"firstName": "Abhi",
"lastName": "A B",
"gender": "male",
"age": 25,
"email": "[email protected]",
"phoneNumber": 8888888888,
"department": "IT",
"address": "Bengaluru"
},
{
"id": 2,
"firstName": "Amogh",
"lastName": "A M",
"gender": "male",
"age": 25,
"email": "[email protected]",
"phoneNumber": 8888888888,
"department": "IT",
"address": "Bengaluru"
},
{
"id": 3,
"firstName": "Harsha",
"lastName": "H A",
"gender": "male",
"age": 25,
"email": "[email protected]",
"phoneNumber": 8888888888,
"department": "IT",
"address": "Bengaluru"
}
]
My home.html is as follows:
<header>Welcome Home</header>
<br />
<div class="container panel panel-default" *ngFor="let employee of employeeList;">
<div class="panel-heading">
<h3 class="panel-title">{{employee.firstName}} {{employee.lastName}}</h3>
</div>
<div class="panel-body">
<div class="col-xs-10">
<div class="row vertical-align">
<div class="col-xs-8 offset-md-2">
<div class="row">
<div class="col-xs-6">
First Name
</div>
<div class="col-xs-6">
: {{employee.firstName}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Last Name
</div>
<div class="col-xs-6">
: {{employee.lastName}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Gender
</div>
<div class="col-xs-6">
: {{employee.gender}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Department
</div>
<div class="col-xs-6">
: {{employee.department}}
</div>
</div>
<div>
<button class="btn btn-primary" (click)="viewEmployee(employee.id)">
View</button>
<button class="btn btn-primary" (click)="editEmployee(employee.id)">
Edit
</button>
</div>
</div>
</div>
</div>
</div>
</div>
My home.ts is as follows:
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import employees from '../employeeDetails/employees.json';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
constructor(private router: Router) { }
public employeeList: {
id: number;
firstName: string;
lastName: string;
gender: string;
age: number;
email?: string;
phoneNumber?: number;
department: string;
}[] = employees;
// tslint:disable-next-line: use-lifecycle-interface
ngOnInit(): void { }
viewEmployee(id): any {
this.router.navigateByUrl(`/view/${id}`);
}
editEmployee(i): any {
this.router.navigateByUrl('/edit');
}
}
My view.html is as follows:
<header>View Page</header>
<br />
<div class="container panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{employee.firstName}} {{employee.lastName}}</h3>
</div>
<div class="panel-body">
<div class="col-xs-10">
<div class="row vertical-align">
<div class="col-xs-8 offset-md-2">
<div class="row">
<div class="col-xs-6">
First Name
</div>
<div class="col-xs-6">
: {{employee.firstName}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Last Name
</div>
<div class="col-xs-6">
: {{employee.lastName}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Gender
</div>
<div class="col-xs-6">
: {{employee.gender}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Age
</div>
<div class="col-xs-6">
: {{employee.age}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Email
</div>
<div class="col-xs-6">
: {{employee.email}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Phone Number
</div>
<div class="col-xs-6">
: {{employee.phoneNumber}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Department
</div>
<div class="col-xs-6">
: {{employee.department}}
</div>
</div>
<div>
<button class="btn btn-primary" (click)="editEmployee()">
Edit
</button>
</div>
</div>
</div>
</div>
</div>
</div>
My view.ts is as follows:
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import employees from '../employeeDetails/employees.json';
@Component({
selector: 'app-view',
templateUrl: './view.component.html',
styleUrls: ['./view.component.css']
})
export class ViewComponent implements OnInit {
constructor(private router: Router, private route: ActivatedRoute) { }
public employeeList: {
id: number;
firstName: string;
lastName: string;
gender: string;
age: number;
email?: string;
phoneNumber?: number;
department: string;
}[] = employees;
employee;
ngOnInit(): void {
this.route.params.subscribe((params) => {
const employeeId = params.id;
this.employee = employees.filter((e) => e.id === +employeeId)[0];
});
}
editEmployee(): any {
this.router.navigateByUrl('/edit');
}
}
My edit.html is as follws:
<header>Edit Page</header>
<div class="container" style="margin-top:1%" action="/view">
<div class="col-md-6 col-md-offset-3">
<div class="form-group">
<label for="exampleInputEmail">First Name</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputPassword">LastName</label>
<input type="text" class="form-control">
</div>
<div class="form-group">
<tr><label for="exampleInputGender">Gender : </label>
<td><input type="radio" id="male" name="gender" value="male" checked="checked">
<label for="male">Male</label><br></td>
<td><input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label></td>
</tr>
</div>
<div class="form-group">
<label for="exampleInputAge">Age</label>
<input type="number" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputEmail">Email</label>
<input type="email" class="form-control">
</div>
<div class="form-group">
<label for="exampleInputPhoneNumber">Phone Number</label>
<input type="text" class="form-control" id="exampleInputPhoneNumber">
</div>
<div class="form-group">
<label for="exampleInputPhoneNumber">Address</label>
<textarea type="textbox" class="form-control" id="exampleInputAddress"></textarea>
</div>
<input type="submit" value="Submit" (click)=editDetails()>
</div>
</div>
My edit.ts is as follows:
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import employees from '../employeeDetails/employees.json';
@Component({
selector: 'app-edit',
templateUrl: './edit.component.html',
styleUrls: ['./edit.component.css']
})
export class EditComponent implements OnInit {
constructor(private router: Router, private route: ActivatedRoute) { }
public employeeList: {
id: number;
firstName: string;
lastName: string;
gender: string;
age: number;
email?: string;
phoneNumber?: number;
department: string;
}[] = employees;
ngOnInit(): void {
}
editDetails(): any {
this.router.navigateByUrl('/edit');
}
}
Please help, I don't have much idea how to proceed. I thought of editing the .json file but didn't know how to do it. Any other methods are also acceptable.
Upvotes: 2
Views: 167
Reputation: 17504
You can't write into JSON file which you are storing in the project just by typescript.
I am assuming that you want to replicate a server and that's why you have stored a json
locally and fetching the value in component.
To write to such JSON file, you need install something like json-server and then make a POST
call to the localhost server. That should give you the expected behavior.
To fetch the json
data, you'll need make a GET
call to the same localhost
server
As mentioned in the comment, if you want to use sesssionStorage
, you can simply save the json into the ParentComponent
as
constructor(){
sessionStorage.setItem('empList',employees);
}
then to use it OtherComponents
, you can do:
ngOnInit(){
const data = JSON.parse(sessionStorage.getItem('empList'));
if (!!data){
// there is no data in sessionStorage
}
}
Upvotes: 1