Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ag-Grid cellRender with Button Click

I am using an angular 5 with ag-grid data table i cant able to trigger a click event from cell using cellRenderer here how am using my ag-grid --> colDefs

this.columnDefs = [
            {headerName: '#', rowDrag: true, width: 75},
            {headerName: 'One', field: 'fieldName',
                cellRenderer : function(params){
                    return '<div><button (click)="drop()">Click</button></div>'
                }
            }
];

drop() {
    alert("BUTTON CLICKEFD")
}

if am using onClick="alert("123")" --> it works, but i cant able to use onClick="drop()" it throws drop of undefined,

i tried this too inside of cellRenderer --> params = params.$scope.drop = this.drop;

if am using gridOptions with angularCompileRows : true it throws an error Cannot read property '$apply' of undefined. Do i need to install ag-grid enterprise ??

like image 645
YuvaMac Avatar asked Jun 09 '18 21:06

YuvaMac


People also ask

How do you add a button to ag-Grid React?

You can make the edit button open an editing dialog and dispatch the new change to the store after the user closes it: Create a custom cell renderer for the button and dialog. In this example, I'll use Material-UI's Dialog . You can choose whatever dialog library you want however.

How do you call a cell renderer on ag-Grid?

Accessing cell renderers is done using the grid API getCellRendererInstances(params) .

How do you render a checkbox on ag-Grid?

To configure the column to have a checkbox, set colDef. headerCheckboxSelection=true . headerCheckboxSelection can also be a function, if you want the checkbox to appear sometimes (e.g. if the column is ordered first in the grid). <ag-grid-angular [columnDefs]="columnDefs" /* other grid options ...


1 Answers

You can use cellRenderer with a button component. If you want to get the click event on the button from the user on the table, just declare the callback function you want to cellRendererParams.

// app.component.ts
columnDefs = [
{
  headerName: 'Button Col 1',
  cellRenderer: 'buttonRenderer',
  cellRendererParams: {
    onClick: this.onBtnClick.bind(this),
    label: 'Click'
  }
},
...
]

The above code is just a small part, check out full example on Stackblitz

like image 121
T4professor Avatar answered Sep 18 '22 01:09

T4professor