9 Replies Latest reply on Jan 5, 2010 4:00 AM by beynar

    How to retrieve selected item component from List?

    beynar Level 1

      Well the thing what I want to do is:

      improve some visual properties of currently active item from List when user select the item.

      What I can't do is retrieve the instance of item that currently selected by user interaction. I'm listening to change or click event on List components which giving me ListEvent.

      I had trying to retrive item object in many various ways but no one returns me an instance of the selected item.

       

      my list looks very simply:

       

      <mx:List>
          <mx:itemRenderer>
              <mx:Component>
                  <mx:HBox>
                      <mx:Label text="{data.firstName + ' ' + data.lastName}"/>
                  </mx:HBox>
              </mx:Component>
          </mx:itemRenderer>
      </mx:List>
      
        • 1. Re: How to retrieve selected item component from List?
          Gregory Lafrance Level 6

          Item renderers are recycled in Flex for better performance, so you either need an event on the itemRenderer component, or else bind itemRenderer component properties and styles to the data and modify those fields in the data.

           

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          • 2. Re: How to retrieve selected item component from List?
            beynar Level 1

            Register a event on ItemRenderer? And how to retrivie those propertis from genereted by item renderer event?

            I'm not sure if I'm understand you correctly.

            Could you give me piece of code Greg

            and thanks for answer

            • 3. Re: How to retrieve selected item component from List?
              Gregory Lafrance Level 6

              I think first I need a more clear description of what you are trying to achieve. How would you like to style or otherwise change the selected elements in the List?

              • 4. Re: How to retrieve selected item component from List?
                beynar Level 1

                I'm building some kind of Internet text communicator.

                User have got a friends list with his contacts. When user select a friend I want to

                - show some additional info about this friend

                - show few actions like (talk, add to talk, delete etc..)

                 

                This elements will take some space in item container so I need to increase size of that item container.

                How do you think, add child is better idea to put new elements into selected item or default make it invisible and manipulate only visible property?

                 

                Thanks,

                Beynar

                • 5. Re: How to retrieve selected item component from List?
                  Gregory Lafrance Level 6

                  Still not clear on exactly what should change in the UI. Describe the UI changes precisely, perhaps describing the user workflow.

                   

                  Read up on item renderers and repeaters.

                  • 6. Re: How to retrieve selected item component from List?
                    beynar Level 1

                    OK, I try to explain it as clear as I can.

                     

                    On a user event like changing selected item of a list I want to:

                    - change size (height) of that selected by user item

                    - change label font of that selcted by user item

                    - add child mxml component to that selected by user item

                     

                    After user select another item of list I want to restore changes of previous item to default and apply this changes on next item (currently selected by user).

                     

                    Referring to piece of code in my first post:

                    For instance, if default height of VBox item is 20 I want to set height property to 50 of selected item if event 'change' or 'click' will occur.

                     

                    Hope, I express it clearly

                     

                    Thanks

                    • 7. Re: How to retrieve selected item component from List?
                      Gregory Lafrance Level 6

                      Anytime you want to affect one of the items in a Flex control like List that recycles itemRenderers, you need to operate on the data. So the data should have fields for height and label font, and a boolean like "selected". Then create an item renderer that accesses these data fields and renders appropriately.

                       

                      For the part where you want to add a child, just have it always be there, but set the visible and includeInLayout properties false unless the "selected" boolean field in the data is true, and set that data field to true when an item is selected.

                      1 person found this helpful
                      • 8. Re: How to retrieve selected item component from List?
                        beynar Level 1

                        I done what you advice me Greg but it's still doesn't work

                        I add to objects which populates list a bindable property height and in item renderer I grab this value and setting as height property of UI component, but it's not reflected at runtime.

                        I'm sure that I retrive height value from objects because I'm printing this value in each item to check it.

                        After ItemRenderer render items I affecting height value in first item. Value in label is changing but it's still not reflected in UI Vbox height.

                         

                        If I put static height value to VBox it's reflected in layout but that don't satisfied me. I have to dynamically manipulate height at runtime.

                         

                        Thanks for help

                        Looking forward to see answers

                        • 9. Re: How to retrieve selected item component from List?
                          beynar Level 1

                          I found solution !

                          Well it's not so brilliant solution but it's work.

                          The point is that changing properties of parent component in Component in ItemRenderer are not reflected in UI. Only changes in children elements are reflected in UI. In my case (post #1) it's label component.

                           

                          However it's works.

                          Thank you Greg !