2
votes

I have a grid filled with >1000 datasets. one column uses a custom itemRenderer which is used to display the value of a foreign key.

All the datasets which are displayed without scrolling are displayed with the right information. But when it comes to scrolling, some datasets will have displayed the wrong value in the itemRenderer. As far as I have understood this, it is because of the reuse of itemRenderers.

But as great as reusing might be, displaying the wrong information cannot be the result. So how do I get rid of this problem?

EDIT: I have managed to get rid of this problem, but I will post some code anyway to point my plan:

<?xml version="1.0" encoding="utf-8"?>

<mx:Script>
    <![CDATA[
        private var dataAccess : DataAccess = DataAccess.getInstance();
        private var foreign : ArrayCollection = new ArrayCollection();

        private function onCreationComplete() : void
        {

            dataAccess.service.getForeignDatasets.addEventListener("result", onGetForeignDatasets);
            dataAccess.service.getForeignDatasets();
        }

        private function onGetForeignDatasets(event : ResultEvent) : void
        {
            foreign = event.result as ArrayCollection;  
            preSelect();
        }   

        //gets the entry from the foreign entity which matches 
        //the foreign key in this.data
        private function preSelect() : void
        {
            for each(var obj : Object in foreign)
                {
                    if(obj.id == data.foreignKey))
                    {
                        value.text = obj.name;
                        return;
                    }
                }

            value.text = "";    
        }

        private function onDataChange() : void
        {
            preSelect();
        }       
    ]]>
</mx:Script>

I left all the uneccessary code...

The code above works and resolves the problem of displaying wrong data.

Any other idea to implement this functionality?

3

3 Answers

0
votes

This happens if your item renderer caches information in private variables (or anywhere else really). An item render gets it's data through the "data" property. It should not use any data not within the "data" property. If you absolutely have to reach outside to get other data (which you really shouldn't do), then make sure you invalidate that data whenever the data property changes (override data to mark a changed flag and then call super.data).

If this doesn't fix your problem, post your item renderer code.

0
votes

Working with custom ItemRenderer for a DataGrid control, I also had this kind of problem. Everything was fine, but when I was scrolling the DataGrid, my ItemRenderers were not showing correct values. It took me a lot of time to understand what's happen. The reason is the way how IremRenderers work. Remember that itemRenderers are recycled. Please read a very good article about Item Renderers.

In short, you have to set the data for ItemRenderer not in constructor or on CreationComplete event, but in other function.

For MX DataGrid- you have to override public function set data, like this:

override public function set data( value:Object ) : void 
{ 
    super.data = value; 
    // your code to set your data 
}

For Spark DataGrid you have to do it in

override public function prepare (hasBeenRecycled)

Hope it can help.

0
votes

useVirtualLayout="false" - disallow itemRenderers re-usage. This is property of the layout of a List base component.

See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/layouts/supportClasses/LayoutBase.html#useVirtualLayout