20

I have a refresh button that is outside the primeNG datatable. How do I programmatically trigger to refresh the datatable?

something like this:

<div class="pull-right">
  <button
    id="FilterBtnId"
    (click)="???">
  </button>
                </div>
<p-dataTable
   #datatable
   [value]="datasource"
   [(selection)]="jobs"
   [totalRecords]="totalRecords"
   selectionMode="multiple"
   resizableColumns="true"
   [pageLinks]="10"
   [rows]="10"
   [rowsPerPageOptions]="[10, 25, 50, 100]"
   [paginator]="true"
   [responsive]="true"
   [lazy]="true"
   (onLazyLoad)="getNewDatasource($event)"
   (onRowSelect)="onRowSelect($event)"
   >
     <p-column [style]="{'width':'40px'}" selectionMode="multiple"></p-column>
     <p-column *ngFor="let col of dt.colDefs" [field]="col.field" [header]="col.headerName" [sortable]="true">
     </p-column>
</p-dataTable>
Cœur
  • 37,241
  • 25
  • 195
  • 267
kimondoe
  • 567
  • 2
  • 9
  • 27

8 Answers8

28

The Angular form guide contains a small trick that could be used as a workaround, it consists on recreating the dom by adding *ngIf to the element to control its visibility

visible: boolean = true;
updateVisibility(): void {
  this.visible = false;
  setTimeout(() => this.visible = true, 0);
}

<button (click)="updateVisibility()">
<p-dataTable *ngIf="visible">
Mauricio Poppe
  • 4,817
  • 1
  • 22
  • 30
  • awesome and simple! – MohamadAbdelfatah Mar 29 '18 at 08:02
  • At first I didn't dare to use this solution because it seemend to simple for me it was just unbelievable at the all it was the simplest and the best solution ever lol I am just wondering why primeface doesnt provide a method that do the trick though =/ – Akyo Jun 27 '18 at 09:23
  • Thanks I used this and it worked, but I am getting ExpressionChangedAfterItHasBeenCheckedError. – P. Huhn yesterday – Phil Huhn Sep 14 '18 at 12:21
  • this is a better solution since making another call to the API is unnecessary. Big up. – Ahmed Hamed May 21 '19 at 11:43
  • 2
    using this trick inside popup caused fast hiding/showing experience that was unpleasant to me. – Hamid Noahdi Feb 20 '21 at 16:33
  • 2
    this works, but unfortunately causes "flicker", the table disappears and reappears at the correct size, probably an OK trade-off, but be aware of it. – BlackICE May 26 '21 at 14:51
6

We can have small trick to update the dataTable here: we can recreating the div by adding *ngIf to the element to control its visibility by this it will update dataTable also.

    visible: boolean = true;
    updateVisibility(): void {
      this.visible = false;
    }
    <button (click)="updateVisibility()">

    <div *ngIf="visible">
        <p-dataTable></p-dataTable>
    </div>
Nitin Jangid
  • 164
  • 1
  • 3
6

If the table is in Lazy mode (loads data dynamically from a server with pagination and so on...) a better way to do this (preserving the page number, sorting, filter and so on) is listed here.

The solution is:

On your component template:

<p-table [value]="yourData" [lazy]="true" (onLazyLoad)="loadUserData($event)" ...

On your component code:

private lastTableLazyLoadEvent: LazyLoadEvent;

loadUserData(event: LazyLoadEvent): void {
    this.lastTableLazyLoadEvent = event;
    // Lots of beautifull data loading code here 
    // (like calling a server trough a service and so on)...
}

...

And when you want to reload the table (e.g. when you insert or delete an item from the server):

this.loadUserData(this.lastTableLazyLoadEvent);
Iogui
  • 1,526
  • 1
  • 17
  • 28
2

I don't know since when it is available but you can call the method createLazyLoadMetadata to create the object you need for the server with sort, filter etc. So you can simply write

@ViewChild('dt') dataTable: Table;
if (this.dataTable) {
   this.loadData(this.dataTable.createLazyLoadMetadata());
}

loadUserData(event: LazyLoadEvent): void {
    this.lastTableLazyLoadEvent = event;
    // Lots of beautifull data loading code here 
    // (like calling a server trough a service and so on)...
}
user2114638
  • 41
  • 1
  • 5
1

Just use a variable that can remove the primeng datatable and reassign it.

visible: boolean = true;
updateVisibility(): void {
  this.visible = false;
}
<button (click)="updateVisibility()">

<div *ngIf="visible">
    <p-dataTable></p-dataTable>
</div>

This is a easy solution.What it does is just remove the dom of the element and reassign it

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
0

If you refresh the list in the component, the table refresh automatically. Example after confirm a delete operation:

import { Component } from '@angular/core';
import { Interface } from '../..//model/interface.model'
import { InterfaceService } from '../../service/interface.service'
import { ButtonModule } from 'primeng/primeng';
import { ConfirmDialogModule, ConfirmationService } from 'primeng/primeng';

@Component({
    templateUrl: './interfaces.component.html'
})
export class InterfacesComponent {

    interfaces: Interface[];

    constructor(
        private interfaceService: InterfaceService,
        private confirmationService: ConfirmationService
    ) { }

    ngOnInit() {
        this.find();
    }

    find() {
        this.interfaceService.query().then(interfaces => this.interfaces = interfaces);
    }

    confirm(id: number) {
        this.confirmationService.confirm({
            message: 'Are you sure that you want to delete this record?',
            accept: () => {
                this.interfaceService.delete(id).then((_interface) => {
                    this.find();
                });            
            }
        });
    }

}
jmfvarela
  • 21
  • 2
0

Pls Try its working for me

refreshTable(){this.loadCustomers(this.lastEvent);}

loadCustomers(event: LazyLoadEvent) { this.lastEvent = event; this.loading = true;  this.service.getAll(event).subscribe( data => { this.loading = false; this.tableData = data['results']; }, error => { this.loading = false; } ); }
Shazvan Hanif
  • 361
  • 3
  • 20
-1

Rendere2 can be helpful

@ViewChild('table') table: Table;

constructor(private renderer: Renderer2) {
}

refreshTable(){
    const dataTableRef = this.renderer.selectRootElement(this.table.el.nativeElement, true);
    dataTableRef.focus();
}

dataTableRef.focus() will refresh the grid

Hamid Noahdi
  • 1,585
  • 2
  • 11
  • 18