6 Replies Latest reply on Feb 4, 2007 5:05 AM by chris.huston.t10

    Bug in my MXML/ActionScript??  Can't find it

    WestSide Level 1
      Hi,

      I posted a similar post prior to this, but I have tried to do this myself but still getting stumped so here is the latest update with code.

      I am trying to set the selectedIndex of my "U.S. States" custom combo box equal to what the user has stored in their record in our database. Someone posted a link to a similar tutorial, but I could not get it to work and I think it seems to be slightly different than what I am doing. When the screen loads, the state the user is associated with should be selected, otherwise set the selectedIndex equal to 0.

      The code:

      public function initStateCB(employeeStateId:Number):void {

      for ( var i:int=0; i< stateCB.dataProvider.length; i++ ) {
      if( employeeStateId == stateCB.dataProvider .stateId ) {
      stateCB.selectedIndex = employeeStateId ;
      }
      else {
      stateCB.selectedIndex = 0;
      }
      }

      } // close function


      <mx:FormItem label="State:">
      <comp:StateCombo id="stateCB" dataProvider="{model.stateList}" labelField="stateName" creationComplete="initStateCB(model.employeeProfileArray[0].stateId)"/>
      </mx:FormItem>

      Any idea why this does not work?

      -Westside

        • 1. Re: Bug in my MXML/ActionScript??  Can't find it
          chris.huston.t10 Level 3
          Your code will only work if the employeeStateId is equal to the last stateId. Because you are using a loop through all the values in hte dataProvider. You could put a break in your if block to prevent the loop from continuing beyond a matched value like this:

          public function initStateCB(employeeStateId:Number):void {

          for ( var i:int=0; i< stateCB.dataProvider.length; i++ ) {
          if( employeeStateId == stateCB.dataProvider.stateId ) {
          stateCB.selectedIndex = employeeStateId ;
          break;
          }
          else {
          stateCB.selectedIndex = 0;
          }
          }

          } // close function

          But it seems that you could avoid using a for loop and just make sure that the stored value is within the range of your dataProvider and assign it directly like this:

          public function initStateCB(employeeStateId:Number):void {
          if( employeeStateId < stateCB.dataProvider.length ) {
          stateCB.selectedIndex = employeeStateId ;
          }
          else {
          stateCB.selectedIndex = 0;
          }
          } // close function

          Vygo
          • 2. Re: Bug in my MXML/ActionScript??  Can't find it
            WestSide Level 1
            Hi,

            Your code works, except it seems to work for me only the first time around. Basically I have a datagrid with a list of employees. You click an employee and you see a detail view of that employee. So the first time I click the employee, the profile loads and the state (California for example) is preselected and all is good.

            However, when I go back to the employee list screen, and click a different employee with a different state, the detail view loads and all the other form fields show the correct data except the state drop down selection of the "previous" user is selected. I traced the array and turned debugging on, etc and each time a detail view is loaded the array is containing the correct value so the array has the right data from what I can see. All of form fields populate correctly, but it appears the first time around it works and every time thereafter it never the state remains the same no matter who I choose. Almost like it cached the first value.

            Any idea why?

            -Westside
            • 3. Re: Bug in my MXML/ActionScript??  Can't find it
              batmitra Level 1
              hi
              i didn't think much on why that happens to you , but have you tried to use a WHILE loop instead of a FOR loop? I ask this because with the for loop you have to force it to break and with the while loop you don't , and the dataprovider is always reseted, so i think your problem should go away using WHILE loop.
              • 4. Re: Bug in my MXML/ActionScript??  Can't find it
                chris.huston.t10 Level 3
                What happens when you add a trace to your initStateCB function and run in debug mode?

                public function initStateCB(employeeStateId:Number):void {
                trace("employee State ID = " + employeeStateId);
                if( employeeStateId < stateCB.dataProvider.length ) {
                stateCB.selectedIndex = employeeStateId ;
                }
                else {
                stateCB.selectedIndex = 0;
                }
                } // close function

                I haven't seen your dataGrid code so I can't say for certain, but it looks like employeeStateId value is not being passed correctly to the function.

                Vygo
                • 5. Re: Bug in my MXML/ActionScript??  Can't find it
                  WestSide Level 1
                  Hi,

                  It turned out to be the loop as batmitra noted. In the loop a "break;" was added.

                  -Westside
                  • 6. Re: Bug in my MXML/ActionScript??  Can't find it
                    chris.huston.t10 Level 3
                    Here is an example of not using a loop to set the combo box. The values are hard-coded but will work the same for data that is stored in a database and accessed via remoting or HTTPService.

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

                    <mx:Script>
                    <![CDATA[
                    private function change_user():void {
                    states_cb.selectedIndex = users_dg.selectedItem.userStateId;
                    }
                    ]]>
                    </mx:Script>

                    <mx:ArrayCollection id="sts">
                    <mx:source>
                    <mx:Object state_name="NONE" state_id="0"/>
                    <mx:Object state_name="AL" state_id="1"/>
                    <mx:Object state_name="AR" state_id="2"/>
                    <mx:Object state_name="AZ" state_id="3"/>
                    <mx:Object state_name="CA" state_id="4"/>
                    </mx:source>
                    </mx:ArrayCollection>

                    <mx:ArrayCollection id="users">
                    <mx:source>
                    <mx:Object user_name="Barbara" userStateId="1"/>
                    <mx:Object user_name="Carol" userStateId="2"/>
                    <mx:Object user_name="Dan" userStateId="3"/>
                    <mx:Object user_name="Ethan" userStateId="4"/>
                    <mx:Object user_name="Frank" userStateId="null"/>
                    </mx:source>
                    </mx:ArrayCollection>
                    <mx:ComboBox x="237" y="9" id="states_cb" labelField="state_name" dataProvider="{sts}" width="80"></mx:ComboBox>
                    <mx:DataGrid x="10" y="10" dataProvider="{users}" itemClick="change_user()" id="users_dg">
                    <mx:columns>
                    <mx:DataGridColumn headerText="User" dataField="user_name"/>
                    <mx:DataGridColumn headerText="state id" dataField="userStateId"/>
                    </mx:columns>
                    </mx:DataGrid>
                    </mx:Application>

                    Vygo