I'm facing this issue while trying to call a service method on the click event of a button that is created using cellRenderer.
I'm using ag-grid with Angular2.
{ headerName: 'Update', field: "update", width: 80, cellRenderer : this.updateRecord }
updateRecord(params) {
var eDiv = document.createElement('div');
eDiv.innerHTML = '<span class="my-css-class"><button class="edit">Edit</button></span>';
var eButton = eDiv.querySelectorAll('.edit')[0];
eButton.addEventListener('click', function() {
this.myService.getAll().subscribe(data => console.log(JSON.stringify(data)))
});
return eDiv;
}
I'm getting the following error :
EXCEPTION: Cannot read property 'getAll' of undefined
Also, I'm not able to access any of my Component variables.
After the grid has created an instance of a cell renderer for a cell it is possible to access that instance. This is useful if you want to call a method that you provide on the cell renderer that has nothing to do with the operation of the grid. Accessing cell renderers is done using the grid API getCellRendererInstances (params).
It doesn't work that way. The string you're returning from your function is not compiled with Angular. ag-grid simply renders that within the grid as a cell renderer. You can handle it with rowClicked event as per below. // 1.
The full list of component types you can provide in ag-Grid are as follows: Cell Renderer : To customises the contents of a cell. Cell Editor : To customises editing of a cell. Date Component : To customise the date selection component in the date filter. Filter Component : For custom column filter that appears inside the column menu.
The renderer is registered to ag-Grid via gridOptions.frameworkComponents. The button click handler is passed to our renderer at run time via cellRendererParams - allowing for a more flexible and reusable renderer.
cellRenderer is in different context, so this
is not accessible in updateRecord
function.
You need to initialize context
parameter in gridOptions
and use it in params
object.
set context
in your gridOptions
object in this way
this.gridOptions.context = {
myService: this.myService
}
Your myService
will be available in context
property.
updateRecord(params) {
var eDiv = document.createElement('div');
eDiv.innerHTML = '<span class="my-css-class"><button class="edit">Edit</button></span>';
var eButton = eDiv.querySelectorAll('.edit')[0];
eButton.addEventListener('click', function() {
params.context.myService.getAll().subscribe(data => console.log(JSON.stringify(data)))
});
return eDiv;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With