Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7015251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:37:39+00:00 2026-05-27T22:37:39+00:00

I’ve got a model class with a bunch of Number variables that change frequently.

  • 0

I’ve got a model class with a bunch of Number variables that change frequently.
They all dispatch their custom events, thus are bindable.

In my UI, a couple of those class instances are bundled into an ArrayList that serves as the dataprovider for a Spark DataGrid.
The class variables change perfectly fine, so problem there.

What I’d like to do now is to change the color formatting of those numbers (the corresponding labels in the gridcoloums to be exact) depending on the data that comes in, so to say change in green when the new value is bigger, change to red when the new value is smaller than the old one.

How can I make this work? I though about some sort of caching of the old value and then compare the old and new one.
Is this the way to do this, if so, how? Or is there another, probably simpler way without the need to cache anything?

Any help would be much appreciated!

Edit:
Based on the example given by @NoobsArePeople2, this is my current code.
First the DataGrid. The dataProvider is an ArrayList that holds objects of myModel class.

<s:DataGrid dataProvider="{_listItems}" >
    <s:columns>
        <s:ArrayList>
        <s:GridColumn dataField="change" headerText="Change" itemRenderer="tableCell">
        <s:GridColumn dataField="bid" headerText="Bid" itemRenderer="tableCell">
  ...
</s:DataGrid>

Now the tableCell renderer;

<s:GridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" width="100%" height="100%">

<fx:Script>
    <![CDATA[
        private var previousData:Number;
        private var labelColor:uint;
                    private var _data:Object;

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

            previousData = _data;
            _data = Number(value);
            invalidateProperties();
        }

        override protected function commitProperties():void {
            super.commitProperties();

            if(previousData < data) {
                labelColor = 0x40c040;
            } else if (previousData > data){
                labelColor = 0xf05050;
            } else
                labelColor = 0xc0c0c0;

            itemLabel.setStyle("color", labelColor);
            itemLabel.text = String(_data); 
        }   
    ]]>
</fx:Script>

<s:Label id="itemLabel"/>
</s:GridItemRenderer>

This works fine so far (opposed to using the data property which throws an undefined error), however, when using this very item renderer for multiple grid columns, it uses the same values for each grid column. What’s that about?

Edit: Sorry, this works only if I directly address the property of the Model, such like data.property, which I can’t do. I need a general item renderer for all columns.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:37:40+00:00Added an answer on May 27, 2026 at 10:37 pm

    The way to go is a custom item renderer. An item renderer will come with a data property (where data is a single value from your data provider) that will get set for you when your data provider changes on the DataGrid. You’ll want to

    1. Add a new private variable previousData of type Object, and
    2. Override the setter for data in your item renderer.

    Your setter for data will look something like this:

    override public function set data(value:Object):void
    {
        // If the data hat not changed, don't do anything
        if (data == value) return;
    
        // Take the current data and store it in previousData
        previousData = data;
    
        // Update the current data to the new value that was passed in
        // BEGIN EDIT
        // data = value; <-- Previous code example is wrong it should be:
        super.data = value;
        // END EDIT
    
        // Tell Flex to update the properties of this renderer
        invalidateProperties();
    }
    

    Then, in your item renderer, override the commitProperties() method like this:

    override protected function commitProperties():void
    {
        super.commitProperties();
    
        // pseudo code follows
        if (previousData < data)
        {
            labelColor = green;
        }
        else if (previousData > data)
        {
            labelColor = red;
        }
        else
        {
            labelColor = black;
        }
    
        label.text = data;
        label.color = labelColor;
        // end pseudo code
    
    }
    

    Alternatively, if you need to access properties of data and don’t want to hard code values this should work (note: I haven’t actually tested this code, but it should work just fine).

    override protected function commitProperties():void
    {
        super.commitProperties();
    
        // This assumes your item renderer subclasses [GridItemRenderer][2]
        var dataField:String = column.dataField;
        var previousValue:Object = previousData[dataField];
        var currentValue:Object = data[dataField];
    
        // pseudo code follows
        if (previousValue < currentValue)
        {
            labelColor = green;
        }
        else if (previousValue > currentValue)
        {
            labelColor = red;
        }
        else
        {
            labelColor = black;
        }
    
        label.text = data;
        label.color = labelColor;
        // end pseudo code
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.