I’m working on a project where I have to use ag-grid for table. I’m using angular for the project. But the problem is I want to show the user’s profile picture and name in one cell in ag-grid like the image I was attached. But I couldn’t render the image. How can I do this? I tried lots of things, but failed. Please help me to find the solution.
JavaScript
x
119
119
1
import { Component, OnInit } from '@angular/core';
2
import { HeaderService } from 'src/app/services/header.service';
3
4
declare function settingsTab(): any;
5
6
function actionCellRenderer(params: any) {
7
let eGui = document.createElement("div");
8
9
let editingCells = params.api.getEditingCells();
10
// checks if the rowIndex matches in at least one of the editing cells
11
let isCurrentRowEditing = editingCells.some((cell: any) => {
12
return cell.rowIndex === params.node.rowIndex;
13
});
14
15
if (!isCurrentRowEditing && params.data != undefined) {
16
eGui.innerHTML = `
17
<button class="action-button edit">
18
<svg xmlns="http://www.w3.org/2000/svg" width="14.778" height="14.908" viewBox="0 0 14.778 14.908">
19
<g id="Icon_feather-edit" data-name="Icon feather-edit" transform="translate(-2.25 -1.938)">
20
<path id="Path_652" data-name="Path 652" d="M8.939,6H4.32A1.32,1.32,0,0,0,3,7.32v9.238a1.32,1.32,0,0,0,1.32,1.32h9.238a1.32,1.32,0,0,0,1.32-1.32V11.939" transform="translate(0 -1.782)" fill="none" stroke="#e0e0e0" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"/>
21
<path id="Path_653" data-name="Path 653" d="M18.929,3.228a1.4,1.4,0,0,1,1.98,1.98L14.64,11.477l-2.64.66.66-2.64Z" transform="translate(-5.041 0)" fill="none" stroke="#e0e0e0" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"/>
22
</g>
23
</svg>
24
</button>
25
<button class="action-button delete">
26
<svg xmlns="http://www.w3.org/2000/svg" width="14.831" height="16.497" viewBox="0 0 14.831 16.497">
27
<path id="Path_654" data-name="Path 654" d="M16.5,6.333l-.722,10.116A1.666,1.666,0,0,1,14.113,18h-6.9a1.666,1.666,0,0,1-1.662-1.548L4.833,6.333M9,9.665v5m3.333-5v5m.833-8.332v-2.5A.833.833,0,0,0,12.332,3H9a.833.833,0,0,0-.833.833v2.5M4,6.333H17.331" transform="translate(-3.25 -2.25)" fill="none" stroke="#e0e0e0" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"/>
28
</svg>
29
</button>
30
`;
31
}
32
return eGui;
33
}
34
function roleCellRenderer(params: any) {
35
let eGui = document.createElement("div");
36
37
let editingCells = params.api.getEditingCells();
38
// checks if the rowIndex matches in at least one of the editing cells
39
let isCurrentRowEditing = editingCells.some((cell: any) => {
40
return cell.rowIndex === params.node.rowIndex;
41
});
42
43
if (!isCurrentRowEditing && params.data != undefined) {
44
eGui.innerHTML = `
45
<div class="form-group" style="height: 30px;">
46
<select name="role" id="role" style="height: 30px; font-weight: 500;">
47
<option value="0">Admin</option>
48
<option value="1" selected>Secretary</option>
49
<option value="2">User</option>
50
</select>
51
</div>
52
`;
53
}
54
return eGui;
55
}
56
57
@Component({
58
selector: 'app-admin',
59
templateUrl: './admin.component.html',
60
styleUrls: ['./admin.component.css']
61
})
62
export class AdminComponent implements OnInit {
63
64
public gridApi;
65
defaultColDef;
66
public rowData: any = [];
67
public searchText: string = "";
68
69
columnDefs = [
70
{
71
headerName: "Name",
72
field: "name",
73
cellRenderer: ({ avatar }) => `<img style="height: 14px; width: 14px" src=${avatar} />`
74
},
75
{
76
headerName: "email",
77
field: "email",
78
cellStyle: {
79
color: '#96A0B5'
80
}
81
},
82
{
83
headerName: "Role",
84
field: "role",
85
cellRenderer: roleCellRenderer
86
},
87
{
88
headerName: "",
89
field: "action",
90
cellRenderer: actionCellRenderer
91
}
92
];
93
94
row = [
95
{
96
"name": "Patrick Roberts",
97
"avatar": "assets/img/patient.png",
98
"email": "fionnapearson@gmail.com",
99
"role": "",
100
"action": ""
101
},
102
103
];
104
105
constructor(private headerService: HeaderService) { }
106
107
ngOnInit(): void {
108
Promise.resolve().then(() => this.headerService.setTitle('Admin Setup'));
109
settingsTab(); // switching settings menu
110
}
111
112
onGridReady(params:any) {
113
params.api.sizeColumnsToFit();
114
this.rowData = this.row;
115
}
116
117
}
118
119
Advertisement
Answer
Pass params
and check params.data.avtar
;
JavaScript
1
2
1
cellRenderer: (params) => `<img style="height: 14px; width: 14px" src=${params.data.avatar} />`
2
Or, consider creating separate component for cellRenderer
https://www.ag-grid.com/angular-data-grid/component-cell-renderer/