views:

790

answers:

3

I have a checkbox in a Flex DataGrid, and when I scroll, other rows are randomly checked/unchecked.

After reading over: http://stackoverflow.com/questions/112036/creating-a-column-of-radiobuttons-in-adobe-flex

it's clear that the itemRenderers are getting recycled, but the problem I have with the solution presented there is it moves info about the view into the model.

Does anyone have a better way of solving it, that doesn't force me to put information for the UI into my actionscript model classes? (in my case, I am converting incoming XML data to actionscript classes, and these are getting bound to my datagrid).

Thanks for the help.

+1  A: 

You could create a subclass of DataGrid that internally stores what rows are checked/unchecked (Array/Collection of Boolean) but you would have a devil of a time keeping that in sync with the dataProvider when it is sorted or filtered. I suppose you could use a Dictionary that is keyed by the object in each index of the dataProvider and valued with a Boolean to indicate whether it's selected. That would at least isolate you from the sorting / filtering issues. This will not work if you have duplicate references in your dataProvider.

Alternatively, you could create a subclass of your ActionScript model class and add the "selected" property to it, then write some simple utility methods to "convert" between the two. That way your View deals only with the "ViewModel" class and other layers (especially the server side) deals only with the real "Model" class.

cliff.meyers
I second the second idea. I usually create a base class for all the model classes that get assigned to grids where I define a boolean isChecked (among few other properties required by my design).
Chetan Sastry
I agree, I think the second option is easier and largely avoids any purest notions of "polluting" the model. However the first approach is kind of interesting and has a potential for reuse.
cliff.meyers
A: 

Adding to what cliff.meyers said, there is a third option of creating a custom IList class as described in this blog post by Alex Harui. It is pretty clever actually, and is cleaner as it doesn't require subclassing the component or polluting your model classes.

Chetan Sastry
A: 

thanks everyone. great tips. unfortunately it was becoming too much overhead to keep the model pure, so i just polluted the model like the link in my original post. :( at least it works.

Chetan, neat idea.. i tried working with this for almost an entire day with no luck though.

brd6644, good thoughts on separating the two model classes.. i might go back and do this later.