5 Replies Latest reply: May 1, 2012 4:48 AM by Harpalsinh Rana RSS

    Programmatically Select Items in a List using service results?

    Aar0nm Community Member

      Hello Adobe Forums, I hope this post finds you well.

      Flex n00ber here and I sure could use some help understanding this process.  It's killin' me!


      I have a ComboBox being populated by a system service array of values. (getSystems())

      I also have a multi-select enabled spark List being populated by a different service. (getIterations())

      The label values for the spark list are just numbers 1 through 15.


      What I'm trying to achieve:


      When I select a system in the cbo box, I need to call a service that returns an array of the values associated with that selected system and then programmatically select those values in the spark list.


      I do have a service that returns an array of that data based on the value of the cboBox.selectedItem.sysID. (getIterationsBySysID())


      What does it take to make this happen?


      I hope I explained this in a meaningful way.




        • 1. Re: Programmatically Select Items in a List using service results?
          selvakumar p



          Just set the selected items property of the list control with the array thet u are getting from the service or

          match the service results with the complete data in list and find the index of each and every item that has been returned from the service and

          push the index values to an array and assign the array to the selected indices property of list .

          This will show all the  index values as selected.

          • 2. Re: Programmatically Select Items in a List using service results?
            Aar0nm Community Member

            So that's the 1000 foot view, you make it sound easy... =/

            Thanks for reading my post selvakumar p.

            Will someone help me understand how to first make an array of results from a service?


            Should I make a new service to ONLY bring back the int's I plan on using for the selected index or is it ok to bring back three values with one being the index int I want to use?


            If three things are coming back into an array all how do I specify I want the first index item to be used as the selected indexes? [0]


            I guess my real disconnect (besides having little AS knowledge) is not knowing the correct way  to generate an array of values based on the change event of a control and then calling that set of values within another control as the selectedIndices.


            I've been loving it (Flex) up until this point, but now I'm thoroughly frustrated.


            I'm going back to google to see if I can find that one tutorial.

            • 3. Re: Programmatically Select Items in a List using service results?
              Aar0nm Community Member

              The computer I'm building this on does not have internet connectivity.

              I'm going to hand jam all of the code I'm using in a sample application I've built for this one problem.


              The setup:


              1 spark List

              1 Combo Box


              One service populates the list.

              One service populates the Combo Box.

              One service passed the variable containing a systemID from the selectedItem in Combo Box.

              The list selectedIndices should be the result of the service with variable call.


              Here's the code I'm using.

              <?xml version="1.0" encoding="utf-8"?>

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:systemservice="services.systemservice.*">



              import mx.controls.Alert;

              import mx.events.FlexEvent;

              import spark.events.IndexChangeEvent;


              protected function list_creationCompleteHandler(event:FlexEvent):void


                getCDRTiterationsResult.token = systemService.getCDRTiterations();



              protected function comboBox_creationComplete(event:FlexEvent):void


              getAllSystemResult.token = systemService.getAllSystem();



              protected function comboBox_changeHandler(event:IndexChangeEvent):void


              getCDRTbySysIdResult.token = systemService.getCDRTbySysId(comboBox.selectedItem.sysID);







              <s:CallResponder id = "getCDRTiterationsResult"/>

              <systemservice.SystemService id="systemService" fault ="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor = "true"/>

              <s:CallResponder id = "getAllSystemResult"/>

              <s:CallResponder id = "getCDRTbySysIdResult"/>

              <s:CallResponder id = "getCDRTiterationsResult"/>



              <s:List id="list" creationComplete="list_creationCompleteHandler(event)" labelField="Iteration">

                <s:AsyncListView list="{getCDRTiterationResult.lastResult}"/>


              <s:ComboBox id="comboBox" creationComplete="comboBox_creationCompleteHandler(event)" labelField="sysName" change="comboBox_changeHandler(event)">

              <s:AsyncListView list="{getAllSystemResult.lastResult}"/>




              Please someone enlighten me on what part I'm missing here.


              And thank the gods it's Friday!


              • 4. Re: Programmatically Select Items in a List using service results?
                Aar0nm Community Member

                Set a result event on the service that gets the list selection items.


                Have that event push the selectedIndices with this 'magical' loop.  LOL (I call it magic because it had me mystified for two days!)


                private function onCatResult(e:ResultEvent):void {
                                var v:Vector.<int> = new Vector.<int>();
                                for(var i:int = 0;i<e.result.length;++i) {
                                    //Alert.show("Selecting " + e.result[i].cdrtID);
                                    v.push(e.result[i].cdrtID -1);
                                MyList.selectedIndices = v;

                I had to subtract one from the pushed value in order to compensate for the zero based index of the returning array.


                Hopefully this will help someone in the future, I certainly struggled with it.




                • 5. Re: Programmatically Select Items in a List using service results?
                  Harpalsinh Rana

                  spark.components.List has spark.components.SkinnableDataContainer in its class hierarchy which dispatches a dataProviderChanged event whenever the dataProvider changes. Unfortunatly there is no [Event] metadata in SkinnableDataContainer that allows using this event in MXML. So, you'll need to create your own custom component that extends List.


                  import spark.components.List;

                  [Event(name="dataProviderChanged", type="flash.events.Event")]
                  public class MyList extends List
                  public function MyList()


                  This works with other List-based components (like DropDownList) too.