Search code examples
actionscript-3mobiledatagridflex4itemrenderer

Limit Flex Spark DataGrid ItemRenderer data to column


I've got a DataGrid in my mobile application (I know, I know but there's currently not other solution for this), that numeric values. Depending on the value of a cell, the text gets colored. My big issue is that this doesn't work very for more than about 5 rows of data. I reckon the issue is that the set text function (in which I format the color) can't keep up with the amount of cell changes and formats the text based on the last updated cell of the row. I thought about a column item renderer so that the renderer only gets the value for that column, and not the whole data of the row.

Is something like that possible?

Just for reference, this is my current item renderer (again, this works fine for few rows, for 5+ of fast changing data, this doesn't work any more, cells get formatted even though their data hasn't changed).

public class ColorGridItemRenderer extends DefaultGridItemRenderer 
{
    public function ColorGridItemRenderer() {
        super();
    }           

    override public function set text(value:String):void {
        if (!value)
            value = "";

        if(Number(value) > Number(text) && text!="")
                setStyle("color", 0x40c040);
            else if(Number(value) < Number(text) && text!="")
                setStyle("color", 0xf05050);
            else 
                setStyle("color", 0xc1c1c1);
        }

        super.text = value;
    }
}

Edit: I just double-checked and have to revert my previous statement. A single row works fine, the second row already messes up the color formatting though. It seems as if the datagrid somehow throws together the values. Doing the color formatting in the set data method shows the exact same effect;

override public function set data(value:Object):void {

        if(value && data) {

            label = value[column.dataField];

            if(value[column.dataField] > _oldVal && _oldVal != 0)
                setStyle("color", 0x40c040);
            else if(value[column.dataField] < _oldVal)
                setStyle("color", 0xf05050);
            else 
                setStyle("color", 0xc0c0c0);

            _oldVal = value[column.dataField];  
        }
        super.data = value;

    }

Edit2: My assumption about the grid messing up the ItemRenderer's data seems to be correct. A simple trace in the set data method (trace("old: " + _oldVal + "new : " + value[column.dataField]);) revealed that somehow, values of the next grid row (in case there are 2) get used as well as _oldVal gets the old value of the next row.


Solution

  • Just for reference: Calling super on the overridden function sets the value to the superclass, thus mixing the values of the rows. Leaving it from the function solves the problem.