我有一个位于primeNG数据表之外的刷新按钮。 如何以编程方式触发刷新数据表?
类似的东西:
<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>
答案 0 :(得分:21)
Angular form guide包含一个可以用作解决方法的小技巧,它包括通过向元素添加*ngIf
来重新创建dom以控制其可见性
visible: boolean = true;
updateVisibility(): void {
this.visible = false;
setTimeout(() => this.visible = true, 0);
}
<button (click)="updateVisibility()">
<p-dataTable *ngIf="visible">
答案 1 :(得分:4)
我们可以在这里更新dataTable的小技巧: 我们可以通过向元素添加* ngIf来重新创建div来控制它的可见性,它也会更新dataTable。
visible: boolean = true;
updateVisibility(): void {
this.visible = false;
}
<button (click)="updateVisibility()">
<div *ngIf="visible">
<p-dataTable></p-dataTable>
</div>
答案 2 :(得分:0)
如果刷新组件中的列表,表将自动刷新。 确认删除操作后的示例:
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();
});
}
});
}
}
答案 3 :(得分:0)
Rendere2 很有帮助
@ViewChild('table') table: Table;
constructor(private renderer: Renderer2) {
}
refreshTable(){
const dataTableRef = this.renderer.selectRootElement(this.table.el.nativeElement, true);
dataTableRef.focus();
}
dataTableRef.focus() 将刷新网格
答案 4 :(得分:0)
如果表格处于懒惰模式(使用分页等从服务器动态加载数据...),则列出了执行此操作的更好方法(保留页码、排序、过滤器等){{3 }}。
解决办法是:
在您的组件模板上:
<p-table [value]="yourData" [lazy]="true" (onLazyLoad)="loadUserData($event)" ...
关于您的组件代码:
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)...
}
...
当您想重新加载表格时(例如,当您从服务器插入或删除项目时):
this.loadUserData(this.lastTableLazyLoadEvent);