ActionScript 2.0 Components Language Reference |
|
|
|
| CellRenderer API > CellRenderer.setValue() | |||
Flash Player 6 (6.0.79.0).
Flash MX 2004.
componentInstance.setValue(suggested,item,selected)
suggested A value to be used for the cell renderer's text, if any is needed.
item An object that is the entire item to be rendered. The cell renderer can use properties of this object for rendering.
selected A string with the following possible values: "normal", "highlighted", and "selected".
Nothing.
Method; takes the values given and creates a representation of them in the cell. This resolves any difference between what was displayed in the cell and what needs to be displayed in the cell for the new item. (Remember that any cell could display many values during its time in the list.) This is the most important CellRenderer method, and you must implement it in every cell renderer.
The setValue() method is called frequently (for example, when a rollover, a selection, column resizing, or scrolling occurs). It is important to remember that a cell might not exist on the Stage and should not always be updated with data when setValue() is called. For example, at any moment, a particular cell may be scrolled out of the display area or it might be reused to render another value. For this reason, you cannot directly reference a specific cell renderer instance in the grid, and you should write if statements in the body of setValue() that allow code to run only if the item parameter is defined and a change has occurred. An undefined item parameter indicates that the cell should be visibly empty and any items in the cell should be assigned a _visible property of false. Cells can be required to be visibly empty temporarily, such as when scrolling occurs in a DataGrid.
If a row is selected and the pointer is over it, the value of the selected parameter is "highlighted", not "selected". This can cause problems if you're trying to make the cell renderer behave differently according to whether the row is in a selected state. To test whether the current row is in a selected state, use the following code:
var reallySelected:Boolean = selected != "normal" && listOwner.selectedNode == item;
The following example shows how to use setValue() and editField() to reference a cell renderer instance in a grid.
Because a cell might not exist on the Stage (it might be scrolled out of the display area or it might be reused to render another value) at any time, you cannot directly reference a specific cell renderer instance in the grid.
Instead, use the data provider to communicate with a specific cell in the grid. The data provider holds all the state information about the grid. To display a given cell as enabled or selected (checked), there should be a corresponding field in the data provider to hold that information. The setValue() method of your cell renderer communicates changes in the data provider's state to the cell. The following is a setValue() implementation from a theoretical cell renderer that renders a check box in the cells:
function setValue(str, itm, sel)
{
/* Assume the data provider has two relevant fields for this cell: checked and enabled.
The form of such a data provider might look like this:
[
{field1:"DisplayMe", field2:"SomeString", checked:true, enabled:false}
{field1:"DisplayMe", field2:"SomeString", checked:false, enabled:true}
{field1:"DisplayMe", field2:"SomeString", checked:true, enabled:true}
]
*/
/* Hide anything normally rendered in the cell if itm is undefined. Otherwise update the cell contents with the new data.
*/
if (itm == undefined){
myCheck._visible = false;
}else{
// redundancy checking
if (myCheck.selected!=itm.checked){
myCheck.selected = itm.checked;
}
if (myCheck.enabled!=itm.enabled){
myCheck.enabled = itm.enabled;
}
}
}
If you want to enable the check box on the second row, you communicate through the data provider. Any change to the data provider (when made through a DataProvider method such as DataProvider.editField()) calls setValue() to refresh the display of the grid. This code would be written in the Flash application, either on a frame, on an object, or in another class file (but not in the cell renderer class file):
// calls setValue() again myGrid.editField(1, "enabled", true);
The following example loads an image in a loader component within the cell, depending on the value passed:
function setValue(suggested, item, selected) : Void
{
/* Hide anything normally rendered in the cell if item is undefined. Otherwise update the cell contents with the new data.
*/
if (item == undefined){
loader._visible = false;
}else{
// clear the loader
loader.contentPath = undefined;
// the list has URLs for different images in its data provider
if (suggested!=undefined){
loader.contentPath = suggested;
}
}
}
The following example is from a multiline text cell renderer:
function setValue(suggested:String, item:Object, selected:Boolean):Void
{
/* Hide anything normally rendered in the cell if item is undefined. Otherwise update the cell contents with the new data.
*/
if (item == undefined){
multiLineLabel._visible = false;
}else{
// adds the text to the label
multiLineLabel.text = suggested;
}
}
The following example is from a radio button renderer. If the item parameter is undefined, then the cell may be scrolled out of the display area and should be visibly empty. An if statement is used to determine if the item parameter is undefined. If the item parameter is undefined, the radio button is hidden by setting its _visible property to false; otherwise, the radio button is updated with the new data and appears.
function setValue(str:String, item:Object, sel:String) : Void {
/* Hide anything normally rendered in the cell if item is undefined. Otherwise update the cell contents with the new data.
*/
if (item == undefined) {
radio._visible = false; }
else {
trace(item.data + " " + item.label + " " + item.state + " " + sel);
radio.label = item.label;
radio.data = item.data;
radio.selected = item.state;
radio._visible = true;
}
}
|
|
|
|