How to Display an Image/Link Inside an Ext JS GridPanel’s Cell
Posted on : 11-01-2010 | By : Loiane | In : ExtJS, Grid
3
This tutorial will walk through how you can display an image/link inside an Ext GridPanel cell using a renderer function.
To explain this approach, I will use a sample GridPanel that displays some dummy contact information (name, phone, birthday and email). Together with email data, we will display a link (mailto) and an email link icon/image.
How to do it
First, we need some data to work with. An ArrayStore with a few dummy records:
//array with data - dummy data
var myData = [
['Meyers, Quyn R.', '(943) 570-5141', 'Proin@nullamagna.ca', '05/13/1990'],
['Whitney, Tad T.', '(547) 743-0343', 'vulputate@acurnaUt.org', '05/10/1987'],
['Lawrence, Flavia J.', '(404) 826-4553', 'dapibus.id@accumsan.ca', '01/05/1988'],
['Morales, Susan I.', '(276) 707-8084', 'tristique@seacmetus.com','03/09/1992'],
['Merrill, Desiree Q.', '(911) 546-0559', 'dictum.cursus@vel.ca', '01/07/1981'],
['Hampton, Willa Y.', '(729) 562-8303', 'nascetur@stellus.ca', '06/17/1991'],
['Brewer, Brynne F.', '(818) 302-4393', 'ligula@ullamcorper.org', '04/20/1989'],
['Marsh, Drew D.', '(645) 671-2779', 'et.euismod.et@eget.ca', '02/13/1990']
];
GirdPanel definition:
// create grid
var grid = new Ext.grid.GridPanel({
store: store,
columns: [
{header: 'NAME', width: 170, sortable: true, dataIndex: 'name'},
{header: 'PHONE #', width: 150, sortable: true, dataIndex: 'phone'},
{header: 'BIRTHDAY', width: 100, sortable: true, dataIndex: 'birthday',
renderer: Ext.util.Format.dateRenderer('d/m/Y')},
{header: 'EMAIL', width: 160, sortable: true, dataIndex: 'email',
renderer: renderIcon }
],
title: 'My Contacts',
autoHeight:true,
width:600,
renderTo: document.body,
frame:true
});
How it works
{header: 'EMAIL', width: 160, sortable: true, dataIndex: 'email', renderer: renderIcon }
The insteresting part in the above code is the trend column’s mail. A renderer function is an interceptor method that can change the grid cell before it is rendered.
//image path
var IMG_EMAIL = '/gridcell-with-image/img/email_link.png';
//renderer function
function renderIcon(val) {
return '<a href="mailto:' + val + '">'+ '<img src="' + IMG_EMAIL + '"> ' + val +'</a>';
}
Inside the renderer function we just put some HTML code to display what we want. Pretty easy!
Now you can create your own renderer functions!
Happy coding!
Download (J2EE project):
http://github.com/loiane/gridcell-with-image
Portuguese: http://www.loiane.com/2010/01/extjs-como-colocar-icone-e-link-nas-celulas-do-grid/




