Reputation: 3798
Is there any api or something using which I can render HTML inside a row. I'am able to bind simple html but my HTML is dynamic and contains some angular directives so, how can I render that HTML in ag-grid.
Upvotes: 6
Views: 45121
Reputation: 11
We can use a CellRenderer function to Create a custom HTML template for each cell as given below. I had a link under my tooltip and I did not want to show the href of the anchor tag for which I have used innerHTML.
{
//field: 'TOOL_TIP',
headerName: 'Tooltip',
filter: 'agTextColumnFilter',
cellRenderer: params => {
var a = document.createElement('div');
a.innerHTML = params.data.TOOL_TIP;
return a;
},
tooltip: (params) => '' + params.value
}
Upvotes: 1
Reputation: 5353
The following outdated solution works for ag-grid < 4.
Set the property angularCompileRows to true on grid options.
This will enable angular compilation on the rows.
Grid Options properties : https://www.ag-grid.com/javascript-grid-properties/index.php
Sample of using angularCompileRows can be found here : https://www.ag-grid.com/angular-grid-cell-template/index.php
However be warry that enabling angularCompileRows slow down the grid. If you have a huge amount of data and use the inifite scroll youi may want to use a cellRenderer in order to return a native dom element with native event binding and use $scope.$apply() to resync with angular.
*For the others version : *
It is possible to build cell renderers, cell editors and filters using Angular. Doing each of these is explained in the section on each.
Although it is possible to use Angular for your customisations of ag-Grid, it is not necessary. The grid will happily work with both Angular and non-Angular portions (eg cellRenderers in Angular or normal JavaScript). From https://www.ag-grid.com/angular-more-details/
Upvotes: 2
Reputation: 1877
You may have to use "cellRenderer" property in ag-grid as follows
Lets assume you have a ag-grid html markup defined as follows
<ag-grid-angular style="width: 900px; height: 115px;" class="ag-theme-fresh" [rowData]="rowData" [columnDefs]="columnDefs"></ag-grid-angular>
Lets assume that you are trying to render images in one of the columns basesd on the input text data. Say that your rowData and columnDef definition in the component.ts file are as follows
public columnDefs = [
{ headerName: 'File Type', field: 'fileType', width: 283 },
{ headerName: 'File Icon', field: 'fileIcon', width: 283, cellRenderer: this.customCellRendererFunc }
];
public rowData = [
{ 'fileType': 'ADOBE', 'fileIcon': 'pdf' },
{ 'fileType': 'WORD', 'fileIcon': 'doc' },
{ 'fileType': 'EXCEL', 'fileIcon': 'xls' }
]
Lets try to display a file icon based on the file type. So my customCellRendererFunc shall be as follows
public customCellRendererFunc(params): string {
let cellContent: string = '';
try {
for (let attItr: number = 0; attItr < params.value.length; attItr++) {
let fileName: string = params.value[attItr].fileIcon;
fileType = fileIcon;
if (fileType === 'pdf') {
imagePath = 'assets/pdf-icon.png';
} else if (fileType === 'doc' || fileType === 'docx') {
imagePath = 'assets/doc-icon.png';
} else if (fileType === 'xls' || fileType === 'xlsx') {
imagePath = 'assets/xls-icon.png';
} else {
imagePath = '';
}
cellContent += '<image src="' +
imagePath + '" title="' + filetype, '"></a> ');
}
} catch (exception) {
console.error(exception);
}
return cellContent
}
Make sure to add your image icons in the assets folder.
Upvotes: 4
Reputation: 1651
use cellRenderer for your custom angular html
import { Component } from "@angular/core";
import { ICellRendererAngularComp } from "ag-grid-angular";
@Component({
selector: 'tooltip-cell',
template: `<ng-template #popTemplate>
<div class="tooltip-renderer">
Created By: {{creator}} <br/>
Created On: {{crDateTime | date:'short'}} <br/>
Revised By: {{revisor}} <br/>
Revised On: {{revDateTime | date:'short'}} <br/>
</div>
</ng-template>
<span [tooltip]="popTemplate" placement="left" container="body" triggers="mouseenter mouseleave dblclick">{{params.value}}</span>` })
export class ToolTipRenderer implements ICellRendererAngularComp {
public params: any;
public creator: any;
public crDateTime: any;
public revisor: any;
public revDateTime: any;
agInit(params: any): void {
this.params = params;
this.creator = params.data["Creator"];
this.crDateTime = params.data["CrDate"];
this.revisor = params.data["Revisor"];
this.revDateTime = params.data["RevDate"];
}
refresh(): boolean {
return false;
}
}
var colDef1 = {
headerName: "Model Level",
field: "ModelLevelTimeSeries.Id.Value",
editable: false,
cellRenderer: "tooltipRenderer",
...
....
}
Upvotes: 3
Reputation: 589
Add to the columnDefs of specific column which cells contain HTML:
cellRenderer: function(params) {
return params.value ? params.value : '';
}
This fools ag-grid to think you are rendering column by your own function (which you technically are).
Upvotes: 16