8 Replies Latest reply on Oct 28, 2011 6:42 AM by Claudiu Ursica

    Auto selecting ItemRenderers in a DataGroup?

    JAXIMFLASH

      I have IconItemRenderers in a DataGroup. I want the items to behave like they do in a List component: i.e. clicking an item selects that item and also deselects any other selected items.

       

      Also the List component has a "selectedItem" property but the DataGroup does not, so when an item is selected in a DataGroup, I would want to make sure I know which item is selected.

       

      What is a good strategy of mimicing the item selecting of the List component for the DataGroup component?

        • 1. Re: Auto selecting ItemRenderers in a DataGroup?
          JAXIMFLASH Level 1

          I am now manually telling the iconItemrenderers to select/deselect, but that doesn't work for a various reasons:

           

          1) I have the DataGroup's layout set its useVirtualLayout property to true. So the IconItemRenderers are reused when I scroll to an unseen page of renderers. If I select an item and then scroll to a new down to view more renderers then one of the new renderers appears to be selected. When I scroll back to the original renderer, that renderer may or may not be selected. So I need a way to select a renderer while still being able to use useVirtualLayout.

           

          2) Sometimes I need to programatically select the next or previous item renderer. The problem with doing it programatically is that because I am using useVirtualLayout, the renderer that I want selected may not have been rendered yet.

           

          Is there no easy way to select/deselect renderers in a DataGroup?

          • 2. Re: Auto selecting ItemRenderers in a DataGroup?
            Claudiu Ursica Level 4

            Keep a selected property in your datamodel. Update selected in there. Inside your renderer update the selected state based on that data. Whenever you scroll the data setter in the renderer gets executed, you have your selected property inside the data Object. Bind or update the renderer based on that...

             

            C

            • 3. Re: Auto selecting ItemRenderers in a DataGroup?
              JAXIMFLASH Level 1

              thanks. That might actually work.

               

              But how do I bind the data using ActionScript since my IconItemRenderers are created in ActionScript. thanks.

              • 4. Re: Auto selecting ItemRenderers in a DataGroup?
                Claudiu Ursica Level 4

                You don't.

                Inside renderer (custom renderer) you override  the data setter.

                 

                override public function set data(value:Object):void

                {

                    super.data = value;

                }

                 

                Inside there after the super call you do something like updating renderer state or whatever you need to make it selected:

                this.selected = data.isSelected; or adjust renderer's state , I don't know how your code looks like, but you get the idea ...

                 

                 

                override public function set data(value:Object):void

                {

                    super.data = value;

                    //assign new state invalidate state whatever you need here

                    // depends on what your custom renderer is based

                }

                 

                Post some code if you encounter any issues.

                C

                • 5. Re: Auto selecting ItemRenderers in a DataGroup?
                  JAXIMFLASH Level 1

                  that works if the renderer gets replaced. But wanting to select a new renderer doesn't work if the new and old renderers are both visible.

                   

                  I was reading somewhere that I may need to utilize ChangeWatcher to bind the selected property. i.e.

                  var watcherSetter:ChangeWatcher =  BindingUtils.bindSetter(updateSelected, data, "bSelected");

                  //where "updateSelected" is the function to call when the selected property changes

                  //where "data" is the data object of the IconItemRenderer

                  //where "bSelected" is the getter/setter property within the data object

                   

                  I had expected that updateSelected() would get triggered when I changed the bSelected property within the data object, but it doesn't. It is new to me so I am most probably doing something wrong.

                   

                  Do you know where I am going wrong?

                  • 6. Re: Auto selecting ItemRenderers in a DataGroup?
                    Claudiu Ursica Level 4

                    Your thing is that you one one item selected. And when you select one others should be deselected.

                    SO when you click or touch the item to be selected yo catch the event and handle it. Moreover you need to update the whole collection with the change. It is up to you how you do it. You can bubble the event to the datagroup for instance.

                     

                    Anyway the idea is to iterate through your items collection and update them accordingly.

                     

                     

                    Once you update them the renderers in use will also update.

                     

                     

                    C

                    • 7. Re: Auto selecting ItemRenderers in a DataGroup?
                      JAXIMFLASH Level 1

                      I was hoping that I could just set the renderer's data "bSelected" property to false and that thru data binding, the "selected" property of the renderer would be set.

                       

                      To mimic data binding thru ActionScript, I have extended the data object with the EventDispatcher class. So now when a property of the data object changes, it dispatches an Event.CHANGE event. I have the renderer listening for the this event. When the event happens, then I have the renderer setting its "selected" property.

                       

                      This works, but I wish I could utilize the ChangeWatcher which seems like a better practice to bind your data using ActionScript.

                      Maybe I'll look into this issue further, but for now it works and I thank you for the assistance!

                      • 8. Re: Auto selecting ItemRenderers in a DataGroup?
                        Claudiu Ursica Level 4

                        You know that bindings are at the end of the day implemented through dispatching events :).

                        C