pisco
01-11-2008, 10:38 AM
Hi, i've just started working with datagrid, and i'm having some trouble trying to do what seems to be a fairly simple task, i have a regular dg, with text only , but the string each cell has could be fairly long, so i tried using the multilinecell class available in the help file, the problem is that the text is populated dinamically so i dont know if a cell will or will not have 2 lines.
This class works nicely if the text is always in 2 lines, if it is in only one line the text doenst center on the cell, like a regular cell does with no adicional cellrenderers.
/************************************************** **************************
Copyright (C) 2005 Macromedia, Inc. All Rights Reserved.
The following is Sample Code and is subject to all restrictions on
such code as contained in the End User License Agreement accompanying
this product.
Class: MultiLineCell
An example of a simple cell renderer class that creates a multiple line
text Field.
************************************************** **************************/
class MultiLineCell extends mx.core.UIComponent
{
private var multiLineLabel; // The label to be used for text.
private var owner; // The row that contains this cell.
private var listOwner; // The List/grid/tree that contains this cell.
//Cell height offset from the row height total and preferred cell width.
//private static var PREFERRED_HEIGHT_OFFSET = 4;
private static var PREFERRED_HEIGHT_OFFSET = 4;
private static var PREFERRED_WIDTH = 100;
// Starting depth.
private var startDepth:Number = 1;
// Constructor. Should be empty.
public function MultiLineCell()
{
}
/*
UIObject expects you to fill in createChildren by instantiating
all the movie clip assets you might need upon initialization.
In this case we are creating one label
*/
public function createChildren():Void
{
// The createLabel method is a useful method of UIObject and a handy way to make labels in components.
var c = multiLineLabel = this.createLabel("multiLineLabel", startDepth)
// Links the style of the label to the style of the grid
c.styleName = listOwner;
c.selectable = false;
c.tabEnabled = false;
c.background = false;
c.border = true;
c.multiline = true;
c.wordWrap = true;
c.autoSize = true;
}
public function size():Void
{
/* By extending UIComponent, which imports UIObject, you get setSize for free,
however, UIComponent expects you to implement size().
Assume __width and __height are set for you now.
You're going to expand the cell to fit the whole rowHeight.
The rowHeight itself is a property of the list type component that we are rendering a cell in.
Since we want the rowHeight to fit two lines, when creating the list type component using this
cellRenderer class, make sure its rowHeight property is set large enough that two lines of text
can render within it.*/
/*__width and __height are the underlying variables
of the getter/setters .width and .height.*/
var c = multiLineLabel;
c._width = __width
c._height = __height
//c.setSize(mint_width, mint_height);
}
// Provides the preferred height of the cell. Inherited method.
public function getPreferredHeight():Number
{
/* The cell is given a property, "owner",
that references the row. It’s always preferred
that the cell take up most of the row's height.
In this case we will keep the cell slightly smaller.*/
return owner.__height - PREFERRED_HEIGHT_OFFSET;
}
// Called by the owner to set the value in the cell. Inherited method.
public function setValue(suggestedValue:String, item:Object, selected:Boolean):Void
{
/* If item is undefined, nothing should be rendered in the cell,
so set the label as invisible. Note: For scrolling List type components
like a scrolling datagrid, the cells are intended to be empty as they scroll
just out of sight, and then the cell is reused again and set to a new value
producing an animated effect of scrolling. For this reason, you cannot rely on
any one cell always having data to show or the same value.*/
if (item==undefined){
multiLineLabel.text._visible = false;
}
multiLineLabel.text = suggestedValue;
trace(multiLineLabel.text + " temos aqui a altura do texto ")
}
// function getPreferredWidth :: only for menus and DataGrid headers
// function getCellIndex :: not used in this cell renderer
// function getDataLabel :: not used in this cell renderer
}
this is the coded provided, my approach was to use autosize= true , and align the ._y value according to the textHeight, or the actual _height of the label, but the thing is all labels traces out the same vale on _height and/or textHeight, even those that i can see that have 2 lines, and i really cant understand why this happens.
Anyone has some ideas on how to sort this one out ?
thnx in advance ;)
This class works nicely if the text is always in 2 lines, if it is in only one line the text doenst center on the cell, like a regular cell does with no adicional cellrenderers.
/************************************************** **************************
Copyright (C) 2005 Macromedia, Inc. All Rights Reserved.
The following is Sample Code and is subject to all restrictions on
such code as contained in the End User License Agreement accompanying
this product.
Class: MultiLineCell
An example of a simple cell renderer class that creates a multiple line
text Field.
************************************************** **************************/
class MultiLineCell extends mx.core.UIComponent
{
private var multiLineLabel; // The label to be used for text.
private var owner; // The row that contains this cell.
private var listOwner; // The List/grid/tree that contains this cell.
//Cell height offset from the row height total and preferred cell width.
//private static var PREFERRED_HEIGHT_OFFSET = 4;
private static var PREFERRED_HEIGHT_OFFSET = 4;
private static var PREFERRED_WIDTH = 100;
// Starting depth.
private var startDepth:Number = 1;
// Constructor. Should be empty.
public function MultiLineCell()
{
}
/*
UIObject expects you to fill in createChildren by instantiating
all the movie clip assets you might need upon initialization.
In this case we are creating one label
*/
public function createChildren():Void
{
// The createLabel method is a useful method of UIObject and a handy way to make labels in components.
var c = multiLineLabel = this.createLabel("multiLineLabel", startDepth)
// Links the style of the label to the style of the grid
c.styleName = listOwner;
c.selectable = false;
c.tabEnabled = false;
c.background = false;
c.border = true;
c.multiline = true;
c.wordWrap = true;
c.autoSize = true;
}
public function size():Void
{
/* By extending UIComponent, which imports UIObject, you get setSize for free,
however, UIComponent expects you to implement size().
Assume __width and __height are set for you now.
You're going to expand the cell to fit the whole rowHeight.
The rowHeight itself is a property of the list type component that we are rendering a cell in.
Since we want the rowHeight to fit two lines, when creating the list type component using this
cellRenderer class, make sure its rowHeight property is set large enough that two lines of text
can render within it.*/
/*__width and __height are the underlying variables
of the getter/setters .width and .height.*/
var c = multiLineLabel;
c._width = __width
c._height = __height
//c.setSize(mint_width, mint_height);
}
// Provides the preferred height of the cell. Inherited method.
public function getPreferredHeight():Number
{
/* The cell is given a property, "owner",
that references the row. It’s always preferred
that the cell take up most of the row's height.
In this case we will keep the cell slightly smaller.*/
return owner.__height - PREFERRED_HEIGHT_OFFSET;
}
// Called by the owner to set the value in the cell. Inherited method.
public function setValue(suggestedValue:String, item:Object, selected:Boolean):Void
{
/* If item is undefined, nothing should be rendered in the cell,
so set the label as invisible. Note: For scrolling List type components
like a scrolling datagrid, the cells are intended to be empty as they scroll
just out of sight, and then the cell is reused again and set to a new value
producing an animated effect of scrolling. For this reason, you cannot rely on
any one cell always having data to show or the same value.*/
if (item==undefined){
multiLineLabel.text._visible = false;
}
multiLineLabel.text = suggestedValue;
trace(multiLineLabel.text + " temos aqui a altura do texto ")
}
// function getPreferredWidth :: only for menus and DataGrid headers
// function getCellIndex :: not used in this cell renderer
// function getDataLabel :: not used in this cell renderer
}
this is the coded provided, my approach was to use autosize= true , and align the ._y value according to the textHeight, or the actual _height of the label, but the thing is all labels traces out the same vale on _height and/or textHeight, even those that i can see that have 2 lines, and i really cant understand why this happens.
Anyone has some ideas on how to sort this one out ?
thnx in advance ;)