Emit Event from cellRendererFramework to parent
Asked Answered
V

1

14

With ag-grid, you can definte your GridOptions.columnDefs with column information including cellRendererFramework. I have a component that I am using for the cellRendererFramework which includes an event that gets triggered from a click on a button within its template. I would like to be able to emit this event to the parent (where the columnDefs are getting defined, and where the ag-grid-angular is initialized from).

I can see that I could just let the event go through ag-grid-angular's cellClicked event... and then I could look at the event to parse out info like the event target and see if it's on the button, etc... but I'm hoping I don't have to do that, and that there is a way to get the event from the cellRendererFramework component ts, up.


My GridOption.columnDefs def for this column looks like this:

{
  headerName: 'Actions',
  cellRendererFramework: ActionCellComponent,
  suppressFilter: true,
}

ActionCellComponent has a template with buttons that have click events, like (click)="actions.deleteSchema($event)" and get picked up in the component ts.

I'm hoping to get events from the ActionCellComponent to the AdminComponent which hosts the ag-grid and the columnDefs without having to parse through the cellClicked event.

Valeda answered 2/5, 2019 at 18:55 Comment(1)
I know this is old, but the way I solve this is to use a service that's defined in the grid as well as the cell editor. The cell editor calls a method in the service, which emits an event that is received by the grid. For example I have a cell editor that can either accept input from the keyboard, or the user can select an item from the list. If the former, the item entered is looked up in the database. If the latter, I suppress the cell changed event using isCancelAfterEnd and emit an event to skip the lookup and use the item selected by the user.Chesser
A
20

I had an issue with finding a clean way to do this. You can use this to obtain a reference to the parent component that has initialised the cellRenderer.

import {GridOptions} from "ag-grid";
constructor(){ 
    this.gridOptions = <GridOptions>{
        context: {
            componentParent: this
        }
    };
} 

Make sure when drawing grid in HTML

<ag-grid-angular #grid-reference [gridOptions]="gridOptions">

Then in your cellRenderer include agInit which will be fire when class is drawn.

public params;

public agInit(params: any): void {
    this.params = params;

    console.log(this.params.context.componentParent); 
    // access to parent functions / variables etc
}

So then for example after an event, if you had public hello: string in the parent you could do the following below.

this.params.context.componentParent.hello = "hi"; // could be function call.

This should then allow you to do as you need interacting between the two. The context is bound.

I hope this is what you are looking for.

Here is ag-grid's documentation.

Assassinate answered 2/5, 2019 at 19:5 Comment(6)
perfect, a bit different than the way I was thinking.. but does what I need. thanksValeda
Yeah probably is other ways, this worked for me so thought I would share :-)Assassinate
This is exactly the solution I was looking for. Thank you for sharing!Wheelwright
No problem at all :-)Assassinate
This is great! You saved me hours...actually, I probably would have given up.Winny
glad it is still handy a few years on :-)Assassinate

© 2022 - 2024 — McMap. All rights reserved.