PDA

View Full Version : Attach Movieclip to Datagrid Cell (and detect mouse click on cellRenderer)



EssenceLord
August 28th, 2008, 10:10 AM
Not to certain how many people may need this script, but lo and behold I had need of it for my project. So, here's my script on how to add a movie clip to a Datagrid Cell via CellRenderer

1) Create an Actionscript file (*.as) and call it IconCellRenderer

2) Put the following code inside that file:


package
{
import fl.controls.listClasses.CellRenderer;
import flash.utils.getDefinitionByName;

public class IconCellRenderer extends CellRenderer
{
public function IconCellRenderer()
{
// -------------- Attach MovieClip -----------------
var instance = new (getDefinitionByName("MyMovie")); // Create Instance of "MyMovie"
this.addChild(instance); // Add this instance to the CellRenderer
instance.name = "MyMovieInstance"; // Name this new instance

// Position Movie in Cell
instance.x += 8;
instance.y += 3;
}
}
}
3) Now set the cellRender for the cells that you want this to apply to:

MyDataGrid.getColumnAt(MyDataGrid.getColumnIndex("ColumnName")).cellRenderer = IconCellRenderer; // load IconCellRenderer


Extra Tid Bid:
Here is a little extra on detecting when someone clicks on a cell utilizing the above cellRenderer: (I'm sure there's a better way to do it, but here you go)

1) Add Event Listener to your datagrid to detect a mouse click
MyDataGrid.addEventListener(MouseEvent.CLICK, onClickCell);

2) Here's the function called upon the Mouse Click:

function onClickEvent (E:Event)
{
// if a record is selected
if (eventGrid.selectedIndex != -1)
{
// Check if user clicked on the Info Icon CellRender of the selected Row
var Row = eventGrid.selectedIndex; // get selected row
var Col = eventGrid.getColumnIndex("Info"); // the column of cells with IconCellRenderer
if (E.target == eventGrid.getCellRendererAt(Row, Col))
MyFunction (); // function you want called when cellRenderer is clicked
}
}

dubbeat
September 1st, 2009, 06:17 AM
Fair play to ya man.

I've known a ton of people who have pulled their hair out trying to do this

icono
October 17th, 2009, 11:42 PM
This is by far the simpliest way to do this I've seen on the net. I know absolutly nothing about Classes, especially ones that extend other classes but was still able to get this to work. I am looking for a way to pass the name of the movieclip I want to attach to function inside the class. I tried this as an obvious fix, but it doesn't work.



public function IconCellRenderer(mcname:String)
{
// -------------- Attach MovieClip -----------------
var instance = new (getDefinitionByName(mcname)); // Create Instance of "MyMovie"


then:



DG.getColumnAt(DG.getColumnIndex("Remove")).cellRenderer = IconCellRenderer("removeArrow_mc"); // load IconCellRenderer


But it throws the error: Type Coercion failed: cannot convert "removeArrow_mc" to IconCellRenderer.

batis
January 14th, 2010, 05:37 AM
how to add different movieclips to different rows? this example adds movieclip to entire column.

joselalupa
August 22nd, 2011, 11:25 PM
weird....i even copy-paste code but nothing appear to be rendering in the cell