2 Replies Latest reply on Nov 9, 2007 7:39 PM by cmarkiewicz

    ComboBox - value disappears...

    cmarkiewicz
      I have a ComboBox in a DataGrid. Here's the behavior I'm seeing: The screen loads initially - the ComboBox is populated and the selected item is shown. If I click on the ComboBox, then move my mouse away and click on some other cell - without physically re-selecting the same item in the list - then the selected item is no longer the selected item. It defaults to the top item in the list.

      In my application, the DataGridColumns shown below. (the value of editorDataField attribute is defined in my component...). The dataField (productCode) is an item in the ArrayCollection which populates the DataGrid.
      <mx:DataGridColumn dataField="productCode" headerText="Product Code"
      headerWordWrap="true" editorDataField="valueSelected"
      width="80" itemRenderer="ProductCodeRenderer2" rendererIsEditor="true" />

      The issue is somewhere in my component, i assume. The code is below. One note - i display either a ComboBox or a Label - depending on another value in the same row of data (nonPartFlag).
      When I debug this app, i check the productCode value (the value that is the selected item in the ComboBox) - and it has a value of null when i see this behavior. So that's why it defaults to the top item, but why is it getting a value of null?
      One other note - this only happens when i first touch a given ComboBox. If I select a value, then i go back and do the behavior described above, it works correctly.

      <?xml version="1.0" encoding="utf-8"?>
      <!-- mxmlcomponents/TypeComboBoxRenderer.mxml -->
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml"
      horizontalScrollPolicy="off" verticalScrollPolicy="off"
      >
      <mx:Script>
      <![CDATA[
      // Define a property for returning the new value to the cell.
      [Bindable]
      public var valueSelected:String;


      override public function set data(value:Object):void
      {
      if(value != null)
      {
      super.data = value;
      trace("set data...data.productCode:"+data.productCode);

      //show the comboBox
      if(value.nonPartFlag)
      {
      prodCodeChoices.visible=true;
      prodCodeLabel.visible=false;
      }
      //hide the comboBox and show the label
      else
      {
      prodCodeChoices.visible=false;
      prodCodeLabel.visible=true;
      }
      prodCodeChoices.selectedItem=data.productCode;
      }
      }

      public function changeHandler(event:Event): void
      {
      trace("changeHandler...prodCodeChoices.selectedItem:"+prodCodeChoices.selectedItem);
      valueSelected = String(prodCodeChoices.selectedItem);
      }
      ]]>
      </mx:Script>

      <mx:ComboBox id="prodCodeChoices" dataProvider="{data.productCodeArray}" width="100%"
      change="changeHandler(event)"
      creationComplete="prodCodeChoices.selectedItem=data.productCode"
      />
      <mx:Label id="prodCodeLabel" text="{data.productCode}" />

      </mx:Canvas>
        • 1. Re: ComboBox - value disappears...
          ntsiii Level 3
          This is the problem
          prodCodeChoices.selectedItem=data.productCode;

          selecteItem rarely is usable because it must be a reference to an item in the dataProvider. That is the dataProvider of the *ComboBox*.

          You cannot set the item of a combo box by giving it a *value* which is what you are trying to do.

          Instead, loop over the cbo dataProvider, and compare the appropriate property values to data.productCode. when you get a match, use the loop index to set the selectdIndex.

          You might want to use an extended combo box that encapsulates this functionality. There are several out there.

          Tracy
          • 2. Re: ComboBox - value disappears...
            cmarkiewicz Level 1
            Tracy -

            Makes sense. I wasn't aware that I shouldn't be using selectedItem that way with a ComboBox.

            Thanks again for your time.
            Chris