使用angular2的ag-grid外部过滤器

ag-grid external filter using angular2

我想用 angular2 在 ag-grid 上实现外部过滤。

农业网格example on github doesn't seem to implement external filters and 。有没有办法在带有 angular2 的 ag-grid 中实现外部过滤器?

我有以下内容:

模板:

<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;
    }
}

我遇到的问题是 externalFilterPass 中的 this 指的是 ag-grid 节点,我无法访问 class 属性。

您是否在 class 上方添加了 @Injectable()?

还有: 像这样重写你的 gridOptions 函数:

        ..., doesExternalFilterPass: (node) => { return this.doesExternalFilterPass(this, node); }, ...

并在您的 class 中使用:

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;
    }
}

在构造函数中,而不是

this.gridOptions = {
   ...
   isExternalFilterPresent: this.externalFilterPresent,
   doesExternalFilterPass: this.externalFilterPass
}

尝试

this.gridOptions = {
   ...
   isExternalFilterPresent: this.externalFilterPresent.bind(this),
   doesExternalFilterPass: this.externalFilterPass.bind(this)
}

现在可以从 ag-grid 方法中访问组件上下文,this 将如您所愿。

来源: