Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ag-grid external filter using angular2

I want to implement external filtering on ag-grid with angular2.

The ag-grid example on github doesn't seem to implement external filters and a similar question has not been answered. Is there a way of implementing external filters in ag-grid with angular2?

I have the following:

Template:

<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)">

Component:

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

The problem I am having is that this in the externalFilterPass refers to the ag-grid node and I have no way of accessing the class property.

like image 372
Jason Avatar asked Sep 22 '16 11:09

Jason


People also ask

How do you add custom filters to Ag grid?

Angular Data Grid: Custom Filter Create your own custom filter. Create your own custom floating filter. Create your own custom date component and customise your own date picker (applies to the provided date filter and the date floating filter).

How do you set the filter value on Ag grid?

The Athlete column has filter=true which defaults to the Set Filter as this example is using ag-Grid Enterprise. The Country column is explicitly configured to use the Set Filter using filter='agSetColumnFilter' . All other columns are configured to use the Number Filter using filter='agNumberColumnFilter' .

How do you enable filters for fields in Ag grid?

If you want to enable filters on all columns, you should set a filter on the Default Column Definition. The following code snippet shows setting filter=true for all columns via the defaultColDef and then setting filter=false for the Sport column, so all columns have a filter except Sport.

How do you get gridOptions on Ag grid?

Access the Grid & Column API You can access the APIs in the following ways: Store them from the gridReady event - they'll be available via the params argument passed into the event. Provide a gridOptions object to the grid pre-creation time. Post-creation the APIs will be available on the gridOptions object.


1 Answers

In the constructor, instead of

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

try

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

Now the component context will be accessible from within the ag-grid methods, and this will be what you expected it to be.

Source: https://stackoverflow.com/a/41965934/6432429

like image 118
Matthew Hinea Avatar answered Sep 19 '22 11:09

Matthew Hinea