我想用angular2在ag-grid上实现外部过滤。
ag-grid example on github似乎没有实现外部过滤器和a similar question has not been answered。有没有办法在ag-grid中使用angular2?
实现外部过滤器我有以下内容:
模板:
<input (keyup)="updateFilters($event)" name="filterAgreementNumber" #filterAgreementNumber/>
<ag-grid-ng2 #agGrid
style="width: 100%;"
[style.height.px]="height"
class="ag-fresh"
[gridOptions]="gridOptions"
[rowData]="promises"
(window:resize)="onResize($event)">
组件:
export class PromisesListComponent {
private gridOptions: GridOptions;
private promises: Promise[];
filterAgreementNumber = '';
constructor(private promisesService: PromisesService) {
this.gridOptions = {
rowData: this.promises,
columnDefs: this.createColumnDefs(),
enableColResize: true,
enableSorting: true,
enableFilter: true,
isExternalFilterPresent: this.externalFilterPresent,
doesExternalFilterPass: this.externalFilterPass,
}
updateFilters(event: any) {
this.filterAgreementNumber = event.target.value; //correctly assigns property
this.gridOptions.api.onFilterChanged();
}
externalFilterPass(node: any) {
console.log(this.getFilterAgreementNumber); //undefined
if (this.filterAgreementNumber && this.filterAgreementNumber.length > 0)
if (node.data.AgreementCode.indexOf(this.filterAgreementNumber) === -1)
return false;
return true;
}
}
我遇到的问题是this
中的externalFilterPass
指的是ag-grid节点,我无法访问类属性。
答案 0 :(得分:11)
在构造函数中,而不是
this.gridOptions = {
...
isExternalFilterPresent: this.externalFilterPresent,
doesExternalFilterPass: this.externalFilterPass
}
尝试
this.gridOptions = {
...
isExternalFilterPresent: this.externalFilterPresent.bind(this),
doesExternalFilterPass: this.externalFilterPass.bind(this)
}
现在可以从ag-grid方法中访问组件上下文,this
将是您所期望的。
答案 1 :(得分:0)
你在课堂上面添加了@Injectable()吗?
也: 像这样重写你的gridOptions函数:
..., doesExternalFilterPass: (node) => { return this.doesExternalFilterPass(this, node); }, ...
并在您的班级中使用:
private doesExternalFilterPass(gcs, node): boolean {
if (<GridRowStatus>gcs.gridActionsService.filter) {
console.log(gcs.gridActionsService.filter);
console.log(node.data);
return gcs.rowStatusService.contains(node.data, [gcs.gridActionsService.filter]);
} else {
return true;
}
}