Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use splice in angular 5

I have created a table that displays data fetched from the database. The TS part that fetches the data looks something like:

this._appService.getVisitData().subscribe((data) => {
  this.checkinTemp = data;
  // Calling the DT trigger to manually render the table
  this.dtTrigger.next();
});

The table gets generated and I have added an edit and delete function, my view code:

<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="row-border hover">

  <thead>
  <tr>
    <th>ID</th>
    <th>Visitor Name</th>
    <th>Department</th>
    <th>Purpose</th>
    <th>Schedule Date Time</th>
    <th>Entry Source</th>
  </tr>
  </thead>
  <tbody>
  <tr *ngFor="let data of checkinTemp">
    <td>{{data.id}}</td>
    <td>
      <a  (click)="viewCheckinData(data)"> {{data.f_name}} {{data.l_name}} </a>
      <span>
            <a  (click)="editCheckinData(data)">Edit</a>
        <a  (click)="confirmDelete(data)">Delete</a>
      </span>
    </td>
    <td>
      <span *ngIf="data.department == null || data.department == ''">N/A</span>
      <span *ngIf="data.department !== null ">{{data.department}}</span>
    </td>
    <td>
      <span *ngIf="data.purpose == null || data.purpose == ''">N/A</span>
      <span *ngIf="data.purpose !== null ">{{data.purpose}}</span>
    </td>
    <td>{{data.pre_check_in | date:'short'}}</td>
    <td>{{data.creator_id}}</td>
  </tr>

  </tbody>
</table>


<p-confirmDialog header="Confirmation" icon="fa fa-question-circle" width="425"></p-confirmDialog>

Now when the user presses delete the confirmDelete(data) gets called. The function in the TS file:

confirmDelete(item) {
  this.confirmationService.confirm({
    message: 'Are you sure you want to delete this Visitor?',
    header: 'Confirmation',
    accept: () => {
      this._appService.deleteCheckin(item.id).subscribe(res => {
        // Splice Or something so the page doesnt reload but the data gets removed from the view.
        this.flashMsg.flashMsg('success', 'Deleted', 'Visitor has been deleted.');
      },
      err => {
        this.flashMsg.flashMsg('error', 'Error', 'Visitor has not been deleted.');
      });
    },
    reject: () => {
    },
  });
}

Until now when I delete data, the data gets deleted and the confirmation message pops up, but the data is still displayed in the view until the page gets reloaded. Is there a way to remove the data from the table without the page loading? I looked up splice but had no luck using it in the code.

like image 564
Atul Stha Avatar asked Apr 20 '18 02:04

Atul Stha


People also ask

What is the use of splice () method?

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

What is difference between splice and slice in angular?

Slice is used to get a new array from the original array whereas the splice is used to add/remove items in the original array. The changes are not reflected in the original array in the case of slice and in the splice, the changes are reflected in the original array.

How do you splice an array of objects?

splice() JS Array Method. The splice() method is a built-in method for JavaScript Array objects. It lets you change the content of your array by removing or replacing existing elements with new ones. This method modifies the original array and returns the removed elements as a new array.


1 Answers

After you have successfully deleted your item from the server, you can delete it from the array using splice(...) like this:

const index = this.checkinTemp.indexOf(item);
this.checkinTemp.splice(index, 1);
like image 180
Daniel W Strimpel Avatar answered Oct 16 '22 18:10

Daniel W Strimpel