The problem is far more subtle than Doug explains here, and in fact
his sample code won't necessarily work.  (Note Doug's sample code is
the canonical sample code--you'll see exactly the same thing
repeatedly, but it's broken.)

Here is the problem code:

> > 
> > myCheckBox.selected = item[getDataLabel()];
> > 

I had exactly this code in my CellRenderer, and it was causing exactly
the same problem Iain described.  I would actually label the problem a
flex/action script bug, but at least it is VERY unexpected behavior.

The problem is, if item[getDataLabel()] is undefined, then this code
does nothing to myCheckBox.selected!!!  It leaves it just how it was,
whether its current value is true or false.  So when Iain scrolls down
in his list, if the new item cell is undefined, since the cell
renderer is being reused, it will be left at the value that was set
when he clicked on the first check box.

To fix this problem, you have to check if item[getDataLabel()] is
undefined.  Something like this:

if (item[getDataLabel()] != undefined) {
    box.selected = item[getDataLabel()];
} else {
    box.selected = false;
}

(I try to avoid fancy but unreadable ? : syntax.)

My example is in 1.5--I don't know if 2.0 is different.


Greg
[EMAIL PROTECTED]




--- In [email protected], "Tracy Spratt" <[EMAIL PROTECTED]> wrote:
>
> Oops, missed that it was 1.5, thanks for the clarification. 
> 
> Tracy
> 
>  
> 
> ________________________________
> 
> From: [email protected] [mailto:[EMAIL PROTECTED] On
> Behalf Of Doug Lowder
> Sent: Monday, March 12, 2007 2:05 PM
> To: [email protected]
> Subject: [flexcoders] Re: Checkbox in Celrenderer Problem
> 
>  
> 
> That's an excellent example for Flex 2. I forgot to mention my reply 
> was based on the original poster's scenario using Flex 1.5. Similar 
> situations with respect to cell renderer re-use though.
> 
> --- In [email protected] <mailto:flexcoders%40yahoogroups.com>
> , "Tracy Spratt" <tspratt@> wrote:
> >
> > Here is a simple example:
> > 
> > http://www.cflex.net/showfiledetails.cfm?
> <http://www.cflex.net/showfiledetails.cfm?> 
> ChannelID=1&Object=File&objectI
> > D=559
> > 
> > Tracy
> > 
> > 
> > 
> > ________________________________
> > 
> > From: [email protected] <mailto:flexcoders%40yahoogroups.com>
> 
> [mailto:[email protected] <mailto:flexcoders%40yahoogroups.com>
> ] On
> > Behalf Of Doug Lowder
> > Sent: Monday, March 12, 2007 1:05 PM
> > To: [email protected] <mailto:flexcoders%40yahoogroups.com> 
> > Subject: [flexcoders] Re: Checkbox in Celrenderer Problem
> > 
> > 
> > 
> > It's not a bug. Cell renderers are created only for visible objects 
> > in a list control, not for every single item in the list. Renderers 
> > get reused for items in the list, which provides a performance gain 
> > compared to creating a renderer object for every list item. What 
> you 
> > are seeing is a renderer that has been reused for multiple items, 
> but 
> > hasn't been told completely how to transfer its data to and from 
> the 
> > list item. The result is seemingly unpredictable behavior.
> > 
> > You need to set the state of your renderer's checkbox in the 
> setValue
> > () method, and set the value of the dataprovider field when the 
> user 
> > clicks on the checkbox to change its state. Assuming your 
> > dataprovider has some boolean field that corresponds to the state 
> of 
> > your checkbox control, then in setValue() you should have something 
> > like the following, where "item" is the data object passed to the 
> > function:
> > 
> > myCheckBox.selected = item[getDataLabel()];
> > 
> > In the checkbox's click handler, you should have something like the 
> > following that sets the value of a field in the dataprovider 
> > according to the checkbox state:
> > 
> > listOwner.editField(
> > getCellIndex().itemIndex, getDataLabel(), myCheckBox.selected);
> > 
> > The listOwner, getCellIndex, and getDataLabel properties are values 
> > that get passed to all cell renderers.
> > 
> > HTH,
> > Doug
> > 
> > --- In [email protected]
> <mailto:flexcoders%40yahoogroups.com>  <mailto:flexcoders%
> 40yahoogroups.com>
> > , "digital_eyezed" <flexcoders@> 
> > wrote:
> > >
> > > Hi,
> > > 
> > > I have a cellrenderer for a datagrid which has a simple checkbox 
> in 
> > it.
> > > 
> > > My problem is, when I click the checkbox, another checkbox (which 
> is
> > > outside the scroll area of the datagrid) is also checked and so on
> > > every row which is equal to the rowcount of the datagrid.
> > > 
> > > Is this a bug?
> > > 
> > > How do I get around this problem?
> > > 
> > > Best Regards,
> > > 
> > > Iain (Flex 1.5)
> > >
> >
>

Reply via email to