Creating a variable row height item renderer on a ComboBox control in Flex

The following example shows how you can create a variable row height item renderer on a Flex ComboBox control by setting the variableRowHeight property on the ComboBox control’s dropdown property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/07/04/creating-a-variable-row-height-item-renderer-on-a-combobox-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">
 
    <mx:Script>
        <![CDATA[
            import mx.events.DropdownEvent;
 
            private function comboBox_open(evt:DropdownEvent):void {
                evt.currentTarget.dropdown.variableRowHeight = true;
            }
        ]]>
    </mx:Script>
 
    <mx:Array id="arr">
        <mx:Object name="Baltimore Orioles" abbr="BAL" />
        <mx:Object name="Boston Red Sox" abbr="BOS" />
        <mx:Object name="Chicago White Sox" abbr="CWS" />
        <mx:Object name="Cleveland Indians" abbr="CLE" />
        <mx:Object name="Detroit Tigers" abbr="DET" />
        <mx:Object name="Kansas City Royals" abbr="KC" />
        <mx:Object name="Los Angeles Angels of Anaheim" abbr="LAA" />
        <mx:Object name="Minnesota Twins" abbr="MIN" />
        <mx:Object name="New York Yankees" abbr="NYY" />
        <mx:Object name="Oakland Athletics" abbr="OAK" />
        <mx:Object name="Seattle Mariners" abbr="SEA" />
        <mx:Object name="Tampa Bay Devil Rays" abbr="TB" />
        <mx:Object name="Texas Rangers" abbr="TEX" />
        <mx:Object name="Toronto Blue Jays" abbr="TOR" />
    </mx:Array>
 
    <mx:ComboBox id="comboBox"
            dataProvider="{arr}"
            width="140"
            labelField="name"
            open="comboBox_open(event);">
        <mx:itemRenderer>
            <mx:Component>
                <mx:Text selectable="false"
                        toolTip="{data.name} ({data.abbr})" />
            </mx:Component>
        </mx:itemRenderer>
    </mx:ComboBox>
 
</mx:Application>

View source is enabled in the following example.

10 thoughts on “Creating a variable row height item renderer on a ComboBox control in Flex

  1. Thanks for many examples.

    i hava a item renderer problem on DataGridColumn.
    There are comboboxes on DataGridColumn and users can choose a column item.
    But when grid is scrolled all DataGridColumns are initialized.
    In my opinion, Flex draw all grid again when scrolled.

    plz tell me the solution.

  2. If you’re trying to do this same thing without using an inline itemRenderer (e.g., just using the default ListItemRenderer, or a a custom renderer that extends that class), you also need to set the wordWrap property of the dropdown box to true:

    private function comboBox_open(evt:DropdownEvent):void 
    {
         evt.currentTarget.dropdown.wordWrap = true;
         evt.currentTarget.dropdown.variableRowHeight = true;
    }
  3. This was very helpful. Thanks.
    Is there anyway to adjust the height of the dropdown when it opens, other than the rowCount? I have two answers in one of my dropdown; the first is short and the second is long. The comboBox then sets the rowCount to 2 and determines the height of the dropdown by multiplying the rowCount by the height of the first row. This forces the user to scroll to see the entire second item.
    Thanks,
    Steve

    1. @Steve,

      Try setting the dropdown height in the open handler:

      <mx:Script>
          <![CDATA[
              import mx.events.DropdownEvent;
       
              private function comboBox_open(evt:DropdownEvent):void {
                  ComboBox(evt.currentTarget).dropdown.variableRowHeight = true;
                  ComboBox(evt.currentTarget).dropdown.height = 50;
              }
          ]]>
      </mx:Script>

      Because the open event is dispatched after the ComboBox is opened, you may want to set the ComboBox control’s openDuration style to 1ms so the resize isn’t as noticeable.

      Peter

    2. Scott,
      This works for me.

      private var internalDropdownFactory:ClassFactory =
      new ClassFactory(SeparatorList);
      ——————————————————————————-

      public class SeparatorList extends List {

      public function SeparatorList() {
      super();
      itemRenderer = new ClassFactory(SeparatorListItemRenderer);
      showDataTips = true;
      }

      /**
      * @private
      * Determines if the itemrenderer is a separator. If so, return null to prevent separators
      * from highlighting and emitting menu-level events.
      */
      override protected function mouseEventToItemRenderer(event:MouseEvent):IListItemRenderer {
      var row:IListItemRenderer = super.mouseEventToItemRenderer(event);

      if (row && row.data && row.data.hasOwnProperty(‘separator’) && row.data.separator){
      return null;
      } else {
      return row;
      }
      }

      /**
      * Override measuredHeight to maximum possible stageHeightAvailable.
      *
      **/
      override protected function measure():void {
      super.measure();
      if(!stage || !owner is ComboBox || !dataProvider) return;

      var pt:Point = stage.globalToLocal(owner.localToGlobal(new Point(0, 0)));
      var stageHeightAvailable:Number = Math.max(pt.y,screen.height – pt.y) – (2*rowHeight);
      var calcHeightByItems:Number = measureHeightOfItems()+rowHeight;

      measuredHeight = Math.min(stageHeightAvailable,calcHeightByItems);

      // trace(‘*****height:’+height);
      // trace(‘*****y:’+y);
      // trace(‘*****pt.y:’+pt.y);
      // trace(‘*****screen.y:’+screen.y);
      // trace(‘*****screen.height:’+screen.height);
      // trace(‘*****screen.height-pt.y:’+(screen.height – pt.y));
      // trace(‘*****rowHeight:’+rowHeight);
      // trace(‘*****calcHeightByItems:’+calcHeightByItems);
      // trace(‘*****stageHeightAvailable:’+stageHeightAvailable);
      // trace(‘*****measuredHeight:’+measuredHeight);

      }//measure

      }//SeparatorList

  4. @Peter

    That really helped. Two other things I would like to do:
    1. adjust the dropdown height depending on the height of the rows available. (e.g. if I have 2 rows, 1 height=20 and 1 height=60, set to 80, etc.)
    2. adjust the label area height depending on the height of the selected row.

    Steve

Comments are closed.