2

I'm trying to do a CRUD application using angular and spring boot. When trying to delete an item, the table can't be refreshed. Here is my code:

Contrat.component.ts:

<table class="table ">
    <thead>
        <th>ID</th>
        <th> TYPE</th>
        <th>MISSION</th>
        <th> <img src="assets/plus.png"> </th>
    </thead>
    <tbody>
        <tr *ngFor="let contrat of contrats ">
            <td>{{contrat.id}}</td>
            <td>{{contrat.type}}</td>
            <td>{{contrat.name_mission}}</td>
            <td><button class="btn btn-danger" (click)="deleteContrat(contrat)">Delete</button>
                <button class="btn btn-primary">Edit</button></td>
        </tr>
    </tbody>
</table>

Contrat.component.ts:

deleteContrat(contrat) {
    this.contratService.deleteContrat(contrat.id).subscribe((data) => {
        this.contrats = this.contrats.filter(u => u !== contrat);
        this.fetchData();
    }, (error) => {});
}

fetchData() {
    this.contratService.getContrats().subscribe(data => {
        this.contrats = data;
    });
}

The delete service works but the table can't be automatically refreshed.

3
  • Try to set your this.contrats object to null. Commented Feb 28, 2019 at 8:49
  • where exactly i should set it ? Commented Feb 28, 2019 at 8:53
  • Added as answer. Commented Feb 28, 2019 at 8:56

5 Answers 5

4

It is not necessary to reload the data. Instead remove the deleted item from your contrat (I assume it should be contract?) array as follows:

this.contratService.deleteContrat(contrat.id).subscribe( (data)=>{
  const deletedContrat = this.contrats.find(x => x.id === contrat.id);
  this.contrats.splice(this.contrats.indexOf(deletedContrat), 1);
}
Sign up to request clarification or add additional context in comments.

1 Comment

it gets error in contratIndex : Argument of type 'Contrat' is not assignable to parameter of type 'number'.ts(2345)
2

Though the data is changed the table will hold the previous data. So to re-render the new data, the trick is to use *ngIF directive with a boolean. Post delete confirmation, just toggle the boolean value to false and on fetch just make the boolean to true.

  <div *ngIf="isAvailable">
  <table class="table table-hover table-bordered">

This will work.

Comments

1

Well, it should help,

fetchData() {
  this.contrats = null; //or create new contrats object
  this.contratService.getContrats().subscribe(data =>{
  this.contrats = data;
});

And add this to your template side,

<table class="table" *ngIf="contrats">
.
.
.

With this approach you are sure that after deleting an entry from context, the new data is current.

2 Comments

i get the same result, nothing changed :(
If you get same result, that means your getContrats() service is returnig you same context. OR you are not getting data from service, you are getting data from CACHE. Try to poke your browser cache settings.
1

You are not deleting the data. Well you are filtering and populating the same data. so you just have to update the contrats array and that's it. There is no need to call fetch data again.

deleteContrat(contrat)
{
this.contratService.deleteContrat(contrat.id).subscribe( (data)=>{
  this.contrats = this.contrats.filter(u => u.id !== contrat.id);
},(error)=>{
} );
}

Comments

1

thank you guys, I solved it by putting this.fetchData(); out of subscribe()

1 Comment

Your this.fetchData() need to be inside of your subscribe block, what if delete event unsuccessfully at server-side? You remove element at client-side but it exist at server-side...

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.