Flex DataGridColumn width set to the longest value of that column

There was an interesting question on stackoverflow.com today. Some unnamed user asked:

Can we change the width of the datagrid column dynamically by clicking on the border of the column in order to display the complete string which is too long to be displayed and needs to be scrolled ? If so, How ?

Also, how can we ensure that the column width changes dynamically based on the number of characters / length of string; since many a times the data is too long to be displayed. Can we set the column width to take the length of data into consideration before displaying onto the datagrid ?

It looked like a cool practice so I thought I would give it a go. This is what I came up, it doesn’t resize columns on double click but rather automatically when data provider is set/updated.

[sourcecode lang="as3"]
 
 

  
    0 ) {
      // and no SPECIAL child exists:
      if ( getChildByName("$someTempUICToRemoveAfterFinished") == null ) {
       // create new SPECIAL child
       // this is required to call measureText
       // if you use custom data grid item renderer
       // then create instance of it instead of UIComponent:
       var uic:UIComponent = new UIComponent();
       // do not show and do not mess with the sizes:
       uic.includeInLayout = false;
       uic.visible = false;
       // name it to leverage get getChildByName method:
       uic.name = "$someTempUICToRemoveAfterFinished";
       // add event listener:
       uic.addEventListener(FlexEvent.CREATION_COMPLETE, onTempUICCreated);
       // add to parent:
       addChild(uic);
      }
     }
     // return an input:
     return input;
    }

    // called when SPECIAL child is created:
    private function onTempUICCreated(event:FlexEvent):void {
     // keep the ref to the SPECIAL child:
     var renderer:UIComponent = UIComponent(event.target);
     // output - this will contain max size for each column:
     var maxLengths:Object = {};
     // temp variables:
     var key:String = "";
     var i:int=0;
     // for each item in the DP:
     for ( i=0; i maxLengths[key] ) {
         // set it as the longest one:
         maxLengths[key] = cellMetrics.width;
        }
       }
      }
     }

     // apply column sizes:
     for ( key in maxLengths ) {
      for ( i=0; i

  
   
    
    
   
  

 
[/sourcecode]

Sample:

[SWF]/wp-content/uploads/2009/06/TextTest.swf, 450, 240[/SWF]

This code works just fine however it may be not efficient enough when applied to large data providers.

Source code is also available on stackoverflow.com under the original entry.

2 Responses to “Flex DataGridColumn width set to the longest value of that column”

  1. Andrii Says:

    We did the same for our custom spreadsheet component :) to improve performance for large data sets you could measure column width for visible items only (of course, it jumps when bigger element shows up but in this way you don’t depend on how many rows you have – our component could have millions rows of data and still has constant rendering time, autoresizing on double click also implemented)

  2. Abdalla Samy Says:

    Check this example
    http://www.flex4ex.com/flex-datagrid/autofit-column-width-to-fit-the-longest-word/

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>