Sep 28
Clickable links within Extjs grid cells
Posted By: Praveen Ray
Although the extjs grid is an awesome component, IMO, it suffers from one major drawback – it’s inability to place active components within data cells. At least it’s not straightforward. Here’s a simple solution to place clickable links within cells. The trick is to place s which look like links and then intercept rowclick and determine which of many links was clicked on.
Here’s an example of a custom Grid Component:
function MyComponent() { MyComponent.superclass.constructor.call(this); } Ext.extend(MyComponent, Ext.grid.GridPanel); MyComponent.prototype.initComponent = function() { var action_tmpl = new Ext.XTemplate( "<span class='link' id='edit-{id}'>Edit</span>", "<span class='link' id='delete-{id}'>Delete</span>" ); action_tmpl.compile(); var content = { // grid related config columns: [ {header: 'First Name', dataIndex:'fname'}, {header: 'Action', dataIndex:'id', renderer: function(c) { return action_tmpl.applyTemplate({id: id}); }} ] } MyComponent.superclass.initComponent.call(Ext.apply(this,content)); } MyComponent.prototype.afterRender = function() { MyComponent.superclass.afterRender.call(this); this.on('rowclick', this.row_click, this); } MyComponent.prototype.row_click=function(grid, ri, evt) { var rec = grid.getStore().getAt(ri); // See Below for within_el method if(evt.within_el('edit-'+r.id)) { alert('Edit clicked'); } else if(evt.within_el('delete-'+r.id)) { alert('Delete clicked'); } }
The within_el method on Ext.EventObject object looks like this:
Ext.apply(Ext.EventObject, { within_el:function(el) { el = Ext.get(el); if(!el) return false; var evt_xy = this.getXY(); var evt_x = evt_xy[0]; var evt_y = evt_xy[1]; return (evt_x > el.getLeft() && evt_x < el.getRight() && evt_y > el.getTop() && evt_y < el.getBottom()); } });

October 2nd, 2009 at 9:15 am
This looks like exactly what I’ve been looking for. It would be even better for extjs beginners (like me) if you could publish a complete page with the code you’ve created (say based on one of the extjs examples grids). A variation on one of the examples from examples/grid/grid3.js would be great.
October 2nd, 2009 at 2:28 pm
You can extend in one pass, which creates much clearer code:
var MyComponent = Ext.Extend(Ext.grid.GridPanel, {
/* optional */
constructor : function() { /* stuff here */ },
// more methods
});