Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-click not worked inside ag-grid cell

i am working with ag-grid and i need to add some custom links in a cell and want to call ng-click function on it. Here is my code

var columnDefs =[
                {headerName: "ID", field: "id"},
                {headerName: "Template Name", field: "user_template_name"},
                {headerName: "Screen", field: "screen_name"},
                {headerName: "Last Uploaded", field: "created_at"},                            
                {headerName: "Manage", cellRenderer: createCustomLinks }
              ];
  var gridOptions = {
                        angularCompileRows:true,
                        columnDefs: columnDefs,
                        rowData: null,
                        enableSorting: true,
                        enableColResize: true,
           };
 var gridDiv = document.querySelector('#myGrid');
 new agGrid.Grid(gridDiv, gridOptions);             
 gridOptions.columnApi.setColumnVisible('id', false);
 gridOptions.api.sizeColumnsToFit();

 function createCustomLinks(params) {
            var cellHtml = '<a ng-click="openTemplateID('+params.data.id+')">Open</a>';

            var domElement = document.createElement("span");
                domElement.innerHTML = cellHtml;

               params.$scope.openTemplateID = function(id){
                  console.log(id);
               }

            return domElement;
          };

i got the following error

TypeError: _this.$scope is null
setTimeout(function () { _this.$scope.$apply(); }, 0);  
ag-grid.js (line 7415, col 39)
Error: this.parentScope is null
RenderedRow</RenderedRow.prototype.createChildScopeOrNull

Please guide me if i am doing something wrong. Thank you.

like image 567
user19694 Avatar asked Mar 12 '23 06:03

user19694


2 Answers

I had this issue once. We need to define it in the grid options. Setting angularCompileRows to true resolved this issue for me

$scope.gridOptions = {
        columnDefs: $scope.columnDefs,
        rowData: $scope.rowData,
        // all other options 
        angularCompileRows: true
    };
like image 160
Vignesh Subramanian Avatar answered Mar 21 '23 07:03

Vignesh Subramanian


When using ag-grid along with angularJS don't do :

 var gridDiv = document.querySelector('#myGrid');
 new agGrid.Grid(gridDiv, gridOptions);   

Instead use in your HTML

<div ag-grid="gridOptions"></div> // don't forget having grdOptions bind to $scope

This alone will trigger you a new problem : columnApi and api will be undefined at the time of execution. To fix this you will have to use the onGridReady callback :

gridOptions.onGridReady = function(params){
     gridOptions.columnApi.setColumnVisible('id', false);
     gridOptions.api.sizeColumnsToFit();
}
like image 25
Walfrat Avatar answered Mar 21 '23 08:03

Walfrat