« DataGrid Double-Click To Edit | Main | Custom ILists, ComboBox Prompts »

Custom ILists, CheckBoxDataGrid, Merged Arrays

Earlier I published how to do a CheckBoxDataGrid where the selection model uses CheckBoxes instead of mouse clicks. I should have mentioned that the example used the DataGrid's selection logic which meant that the dataprovider item's selected state was not stored in the dataProvider and thus you couldn't sort by whether an item was selected or not.

If you have a selected field in your data, then the code to handle checkbox selection is quite different. You basically shut down the DataGrid's selection code and update the collection instead. You can see that in the source code for this example.

This example goes one step further, however. It assumes that you want to store selection in the dataProvider, but the dataProvider items don't have a selected field and are "immutable". The example code shows how to use a custom IList to merge two arrays or ArrayCollections into one merged collection where the data objects contain fields from both arrays. By doing that you can "add" a selected field to each item.

Custom ILists are a powerful way to change data without having to process each data item. The example code merges the items on-demand instead of up-front thus saving startup time. It would be wasteful to process 1000 items if you only show 7 and the user doesn't scroll past 20.

Anyway, here's the code. Usual caveats apply.

Download Source
Run Demo

Comments

Hi Alex, I just have to say to that I'm very happy you submit's so often on your blog :) your samples have been VERY VERY helpful, Thanks a lot :)
I have a enterprise support program at Adobe but your blog is a very good place to start. :)

Hi Alex, I appreciate everything you do here and on flexcoders immensely but what do you have against View Source? :)

---------------------

Alex responds:

Unfortunately, it takes too much time to publish all of those files in this blog system. I'm not able to multi-select files for upload in MovableType. Also, I don't use FlexBuilder for these small things. I have over 1000 test cases like these on my system from different releases and it is just easier and faster for me to manage them via command line.

Fair enough. Out of curiosity, what app do you write Flex code in besides Flex Builder?

--------------------------
Alex responds:

You have to remember that I was writing 1000's of lines of AS code before there was a Flex Builder. I've actually customized Visual Studio 6 to code color AS. I don't need code-hinting. I build off the command line.

I use FlexBuilder for browser integration and memory profiling, but not for these quick examples. I'm just too old-school....

Great tutorial!

My question would be, how would I accomplish something similar using an XMLList as the source of the data I wish to display?

Dumb question, I know...

--------------
Alex responds:

You could probably merge nodes, but I would recommend converting to object in the factoryfunction. Access to xml data is significantly slower than access to object properties, especially if you are dealing with non-strings.

Hi Alex,
Thanks for your previous answer on the blog below.
http://blogs.adobe.com/aharui/2008/02/checkbox_selection_in_datagrid.html. I have a question pertaining this blog of yours. I see that this sample uses CheckBoxDataGrid2 and didnt see that in the source. Is CheckBoxDataGrid2 same as the one in http://blogs.adobe.com/aharui/2008/02/checkbox_selection_in_datagrid.html

Thanks for your prompt reply once again.

------------------
Alex responds:

Sorry. I just updated the .zip file. Thanks for letting me know.

Hi Alex,
Thanks for all the solutions that you put out here. Your samples are really helpful. I was lookin through the source code and couldn't find CheckBoxDataGrid2.mxml in the download. Is it possible for you to post that as well?..Thanks once again

---------------
Alex responds:

I updated the .zip. Download the source again.

Hi,

It is a good example!!

It can not find CheckBoxDataGrid2.mxml

A error happen for

you maybe need upload CheckBoxDataGrid2.mxml

Thanks

Mark

------------------
Alex responds:

I just updated the .zip. Download the source again.

Hi Alex,

Thanks for contributing your expertise.

I'm using WebServices as a data provider and want to have edible checkboxes represent one of the columns.

I feel like a spoiled child: "do I really have to do all that AS coding?"

Spoiled or not, does Flex3 fix any of the problems? Have you heard whether Adobe is going to make this easier?

Thanks

Scott

-------------------
Alex responds:

This post was about using checkboxes to select items. You can easily use checkboxes as a renderer/editor for a boolean field in your data. I think you can even find examples in the Flexcoders archive.

Hey Alex,
Thanks for all your responses. Is there a way in either of your examples related to checkboxdatagrid to disable the selection of the checkbox for certain cases?

Thanks,
Vinay.

Hi Alex,

Is it possible to navigating thru and selecting/deselecting checkboxes in datagrids (or adg) by just using the arrow keys and spacebar?

Thanks,
Bray

--------------------
Alex responds:

Should be possible. Just catch the space bar and change selected on the correct checkbox.

Hi again Alex,

Found this other sample of yours, http://blogs.adobe.com/aharui/CheckBoxDataGrid/CheckBoxDataGridApp.swf
It works great! How where you able to implement that one?
I couldn't find the right source. Can you give me a link for the source?

Thanks,
Bray

---------------
Alex responds:

I just looked at the source. Looks ok to me.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)