添加%符号并为其着色

时间:2011-09-25 19:55:25

标签: actionscript-3 flex

我有一个包含数字的datagrid列。我如何:

1. add a '%' sign at the end of each number in the column 

2. make the color either red or green depending on if the number is less than or greater than 0, respectively. 

我已经能够做到1或者其他但不能同时做到。这就是我所拥有的,#2而不是#1:

// my datagrid column:
<mx:AdvancedDataGridColumn dataField="change" itemRenderer="itemrenderers.ColorRenderer" />

// my item renderer:
package itemrenderers 
{
import mx.controls.Label;
import mx.controls.dataGridClasses.DataGridListData;
public class ColorRenderer extends Label {
    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
    {
        super.updateDisplayList(unscaledWidth, unscaledHeight);
        if (data && data[DataGridListData(listData).dataField] < 0)
        {
            setStyle( "color", 0xA41330 ); //red
        }
        else
        {
            setStyle( "color", 0x59A336 ); //green
        }
    }
}
}

3 个答案:

答案 0 :(得分:1)

尝试将此方法放在项呈示器类中。它应该满足您的要求:

override public function set data(value:Object):void {
    super.data = value;
    if (value) {
        var fieldValue:Number =
            value[DataGridListData(listData).dataField] as Number;
        text = String(fieldValue) + "%";
        if (fieldValue < 0){
            setStyle( "color", 0xA41330 ); //red
        }else{
            setStyle( "color", 0x59A336 ); //green
        }
    }
}

问候。

答案 1 :(得分:1)

如果您的代码正确地为标签着色,这应该可以。

// my datagrid column:
<mx:AdvancedDataGridColumn dataField="change" itemRenderer="itemrenderers.ColorRenderer" />

// my item renderer:
package itemrenderers 
{
import mx.controls.Label;
import mx.controls.dataGridClasses.DataGridListData;
public class ColorRenderer extends Label {
    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
    {
        super.updateDisplayList(unscaledWidth, unscaledHeight);
        if (data && data[DataGridListData(listData).dataField] < 0)
        {
            setStyle( "color", 0xA41330 ); //red
        }
        else
        {
            setStyle( "color", 0x59A336 ); //green
        }
        text = (data[DataGridListData(listData).dataField] as String) + "%";
    }
}
}

答案 2 :(得分:0)

使用datagrid列样式函数和labelfunction。使用样式函数,您可以切换颜色,使用labelfunction可以为数据添加%字符。