R – Flex DataGrid ItemRenderer Issue

apache-flexdatagriditemrenderer

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?

Best Answer

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.

Related Topic