Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
update method angular json server

I would like to update my table without refreshing my page but I don't know how to do it as I am a beginner. In my example I use the table from angular material.

how do I update my page every time I add a new element ?

service

  public url: string = 'http://localhost:3000/tableInfos';

  constructor(private http: HttpClient) { }

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

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

ts.file

  ELEMENT_DATA!: PeriodicElement[]
  displayedColumns: string[] = ['name'];
  dataSource = new MatTableDataSource<PeriodicElement>(this.ELEMENT_DATA);

  constructor(private tableService: TableService) {}

  ngOnInit(): void {
    this.get();
  }

  get() {
    this.tableService.getTableDetails().subscribe((data:any) => {
      this.dataSource.data = data;
    });
  }

  add(name: any) {
    this.tableService.create(name).subscribe((data:any) => {
      console.log(data);
    });
  }

json

{
  "tableInfos": [
    {
      "id": 1,
      "name": "Hydrogen"
    },
    {
      "id": 2,
      "name": "Helium"
    }
  ]
}

interface

export interface PeriodicElement {
  name: string;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just use this.dataSource.data = res inside the update function in ts.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!