Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ag-grid(Angular 2) Cannot access component fields in CellRenderer

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.

like image 855
Akshay Rana Avatar asked Nov 03 '16 06:11

Akshay Rana


People also ask

How do I access a cell renderer in the grid?

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

Is it possible to return string from a function in AG-grid?

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.

What are the different component types available in AG-grid?

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.

How is the button click renderer registered to AG-grid?

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.


Video Answer


1 Answers

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;
  }
like image 71
Sefa Avatar answered Oct 23 '22 15:10

Sefa