Reputation: 31
I would like to add a button for an ag-grid table column in reactjs at the time column definition. And onclick I need to call a class function. I would like to create onclick event and pass the params value to the function and make an api call from there.
columnDefs = [{
..... {
headerName: "View",
field: "id",
sortable: false,
filter: false,
colId: "view",
cellRendererFramework: function(params) {
return <Button onclick = {
this.handleClick
} > Test < /Button>
},
},
......
];
}
handleClick() {
console.log("some API call and state change");
}
render() {
return ( <
div >
<
div id = "myGrid"
style = {
{
height: "100%",
width: "100%"
}
}
className = "ag-theme-material" >
<
AgGridReact enableSorting = {
true
}
groupSelectsChildren = {
true
}
rowData = {
this.state.organization
}
columnDefs = {
this.columnDefs
}
onGridReady = {
this.onGridReady
} >
<
/AgGridReact>
<
/div>
}
export default OrganizationList;
Upvotes: 3
Views: 21762
Reputation: 464
Instead of using cellRenderer in column definition, use cellRendererFramework so agGridReact will know you are returning jsx element.
e.g:
colDefs = [{ ...{
headerName: "View",
field: "id",
colId: "view",
cellRendererFramework: function(params) {
return <button onClick={ this.handleClick }> Test </button>
},
},
....
}]
also don't forget to bind the cell renderer function to your component class constructor else you will not be able to access this
.
Upvotes: 8