我在 Angular 4 应用程序中使用 primeNG 数据表。我想根据所选字段执行行分组。例如,如果用户单击经销商区域,则数据应按经销商区域分组,如果用户单击经销商状态,则数据应根据经销商状态分组。
参考: https ://www.primefaces.org/primeng/#/datatable/rowgroup
我的视图 (HTML) 文件
<div id ="default"><span (click)="default()">Default</span></div>
<div id ="region"><span (click)="groupByDealerRegion()">Dealer Region</span></div>
<div id ="state"><span (click)="groupByDealerState()">Dealer State</span></div>
<p-dataTable #dt1 [value]="(auditList | orderBy: {property: 'dealerCode', direction: 1})" rowGroupMode="subheader" sortField = '{{groupField}}' groupField='{{groupField}}'
expandableRowGroups="true" id="datatable" name="datatable" exportFilename="Audits-list" [sortableRowGroup]="false" >
<p-header>
<div class="ui-helper-clearfix">
<button type="button" pButton icon="fa-file-o" iconPos="left" label="CSV" (click)="dt.exportCSV()" style="float:left"></button>
</div>
</p-header>
<ng-template pTemplate="rowgroupheader" let-rowData>{{rowData[groupField]}}</ng-template>
<p-column field="dealerCode" header="Dealer Code" ></p-column>
<p-column field="dealerName" header="Dealer Name"></p-column>
<p-column field="auditType" header="Audit Type"></p-column>
<p-column field="auditOnSite" header="Audit On-SIte"></p-column>
</p-dataTable>
组件类
export class AuditListViewComponent implements OnInit {
groupField: string = 'auditType';
@ViewChild(DataTable) dt: DataTable;
constructor(private sessionService : SessionService,private auditService : AuditService ){
}
auditList: AuditDetail[] = [];
ngOnInit(): void {
this.getAuditList();
}
getAuditList(){
this.auditService.getAuditList().then( auditList => {
this.auditList = auditList;
});
}
groupByDealerRegion(){
this.sortedField = "dealerRegion";
this.groupField = "dealerRegion" ;
}
groupByDealerState(){
this.sortedField = "dealerState";
this.groupField = "dealerState" ;
}
default(){
console.log("default() --->>>");
console.log(this.dt);
this.groupField = 'auditType';
}
}
我想将组件的 groupField 变量与数据表的 groupField 属性绑定。我成功实现了静态行分组。但要求是当用户单击分组选项时对数据进行分组。
我还尝试在组件中使用数据表引用并从组件更改数据表属性都不起作用。
this.dt.groupField = 'dealerState';
寻找解决方案。提前致谢。