Search code examples
htmlangulartypescriptsortingangular-datatables

Angular 6 DataTables - Table Items Appear/Disappear Upon Sort or Search


I am using Angular-DataTables 6.0 (found here https://l-lin.github.io/angular-datatables/#/welcome) and I have been running into a reoccurring problem. When a table item is added or deleted, the record either vanishes or reappears upon sorting or searching. Is this possibly because the adding/deleting is occurring from outside the dataTable?

I've tried adding the ' datatable="ng" ' fix many others suggested but that didn't change anything. I also attempted to add a re-render function, but in that case I ran into 'object unsubscribed' errors that I couldn't resolve. For reference, some similar problems can be found here: Similar examples include: (angular : lost data after sort datatable) (Sorting of numbers within brackets in angular datatables) (https://github.com/l-lin/angular-datatables/issues/852)

Here is my code:

HTML:
  <table datatable="ng" [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="table table-hover" id="t1">
                <thead>
                        <tr>
                                <th>
                                    <button id="b5">Asset ID</button>
                                </th>
                                <th>
                                    <button id="b5">Employee ID</button>
                                </th>
                                <th>
                                    <button id="b5">Asset Type</button>
                                </th>
                                <th>View</th>
                                <th>Delete</th>
                              </tr>
                              </thead>
        <tbody>
        <tr *ngFor="let a of assets; let i = index"> 
          <td>{{ a.assetID }}</td>
          <td>{{ a.currentUser }}</td>
          <td>{{ a.assetType }}</td>
          <td><button id="button1" (click)="viewAsset(a)"><i class="fas fa-search"></i></button></td>
          <td><button id="b2" class="btn" (click)="scrapAsset(a)" (mouseenter)="buttonHover(i)" (mouseleave)="buttonHoverLeave(i)"><i class="{{buttonIconArray[i].text}}"></i></button></td>
        </tr>
        </tbody>
        </table>
-elsewhere in the code-
         <button class="btn" id="b1" (click)="addAsset()">Add New Asset</button>&nbsp;

TS

   dtOptions: DataTables.Settings = {};
   dtTrigger = new Subject();

 addAsset()
 {
this.confirmChanges = false;
//Create a new asset:
let a: Asset = {
  assetID: this.assetID,
  currentUser: this.currentUser,
  assetType: this.dropdownMessage,

}
//send a notification to the user that owns the new asset
  let notify: Notice = {
  emplyID: a.currentUser,
  notificationSource: "Asset",
  details: "A new " + this.dropdownMessage + " was added to your assets.",
  timeStamp: new Date()
}

//Add the asset to the server
this.AssetsService.addAsset(a)
      .subscribe(asset => { this.assets.unshift(asset);
                            //All of the inputs need to be emptied
                            this.clearFields(); 
                          })
}

scrapAsset(a: Asset)
{
console.log("The ID is " , a.assetID)
//this.AssetsService.deleteAsset(this.currentAsset).subscribe()
this.confirmChanges = false;



//This deletes the asset from the back-end.
this.AssetsService.deleteAsset(a).subscribe(() => { 
  console.log('test')
  this.assets = this.assets.filter(t => t !== a);
  this.NotificationsService.addNotice(notify).subscribe();
  this.clearFields(); });
  }

dtActivate()
 {
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 7,
order: (this.assets.assetType),
orderClasses: false,
};
this.AssetsService.getAssetsIT().subscribe((assetsImported) => {
this.assets = assetsImported;
this.parseData();
  // Calling the DT trigger to manually render the table
  this.dtTrigger.next();
});
}

This is only a partial section of the code, so if you need to see more, just ask. Thank you for any help you might provide!


Solution

  • I've had the same problem for a long time. After a lot of stack overflow and documentation, I found a solution that fixed my problem. Maybe it helps you too.

    Allow datatable to be destroyed

    ngOnInit() {
      this.dtOptions = {
        destroy: true,
        ...
      };
      ...
    }
    

    The method that received new items (called after edit, insert, add...).

    onReceived(items){
        let isFirst = this.items.length === 0;
        this.items = <itemsModel[]>items;
    
        if (isFirst)
          this.dtTrigger.next();
        else
          this.rerender();
    }
    

    The rerender as in documentation

    rerender(): void {
        this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
            dtInstance.destroy();
            this.dtTrigger.next();
        });
    }
    

    https://l-lin.github.io/angular-datatables/#/advanced/rerender

    I hope that this might help you.