Ok, got it working the way I want.

I decided to use "Letter Gothic Line" font from
http://www.fontparade.com/index.php?page=theme&cat=29 as it looked
better (To my eye) than Monaco.

At the same time as I send my request for data to the server from Flex,
I also send a request for Column Names And Widths. This functions looks
like this (PHP)...

                  $ColumnNamesAndWidths = array();
                 if($result->num_rows > 0)
                 {
                     while($resultObject = $result->fetch_object())
                     {
                         foreach($resultObject as $key=>$value)
                         {
                             if(!array_key_exists($key,
$ColumnNamesAndWidths))
                             {
                                 $ColumnNamesAndWidths[$key] =
max(strlen($key), strlen($value));
                             }
                             else // Already have that object in the
array
                             {
                                 $currentWidth = max(strlen($key),
strlen($value));
                                 if($currentWidth >
$ColumnNamesAndWidths[$key])
                                 {
                                     $ColumnNamesAndWidths[$key] =
$currentWidth;
                                 }
                             }
                         }
                     }
                 }

So, now I get an object back in Flex with the names of the columns as
properties and the max number of characters for each column as values.

In Flex, I iterate through this object and create columns from the
properties and set widths from the values...

var myDataGridColumnsArray:Array = new Array();
var ColumnNamesAndWidths:Object = event.result as Object;
for(var columnName:String in ColumnNamesAndWidths)
{
     var currentColumn:DataGridColumn = new DataGridColumn(columnName);
     currentColumn.setStyle("fontFamily", "LetterGothic");
     currentColumn.setStyle("fontSize", 11);
     currentColumn.setStyle("headerStyleName", "myDataGridHeader");
     currentColumn.headerText = columnName;
     currentColumn.width = (ColumnNamesAndWidths[columnName] as int) * 7
+ 10; // Add 10 pixels to make it look better
     currentColumn.dataField = columnName;
     currentColumn.itemRenderer = new ClassFactory(Text);

     myDataGridColumnsArray.push(currentColumn);
}
myDataGrid.columns = myDataGridColumnsArray;


HTH.




Steve

--- In flexcoders@yahoogroups.com, "valdhor" <valdhorli...@...> wrote:
>
> I never found any examples on the web.
>
> I am trying to do this now and have my own strategy which may or may
not be the best. In my case, my Data Grid has a max of around 30 rows -
I don't think my strategy would work for large data sets.
>
> So, what I am thinking is to use a mono-spaced embedded font for the
data grid (I am using Apple's Monaco TrueType font). Seeing as I know
the font size I will be using and each character uses the same width, I
can safely use a simple calculation to get the width of the column. I
send a separate request to the server that returns an object that
contains the max character length for each of the fields. In FLex I
create columns from this object and set the datagrid's columns to this
array.
>
> Once I have this working, I will post an example.
>
>
>
> --- In flexcoders@yahoogroups.com, Alex Harui aharui@ wrote:
> >
> > I don't know of any, but this question has been asked so many times,
there's probably an example if you search the web.
> >
> >
> > On 5/25/10 10:36 AM, "method_air" loudjazz@ wrote:
> >
> >
> >
> >
> >
> >
> > Any 'resize data grid columns to content' code examples?
> >
> > --- In flexcoders@yahoogroups.com
<mailto:flexcoders%40yahoogroups.com> , Alex Harui <aharui@> wrote:
> > >
> > > The column has to be the same size for every row, but each column
can have different widths.  It would be rare to set it from the
itemrenderer, usually some other code accesses datagrid.column[i].width.
> > >
> > >
> > > On 5/19/10 11:02 AM, "method_air" <loudjazz@> wrote:
> > >
> > >
> > >
> > >
> > >
> > >
> > > How do you vary the data grid column widths? Setting the width
property inside the item renderers to different values is not working.
> > >
> > > Thanks,
> > >
> > > Philip
> > >
> > >
> > >
> > >
> > >
> > >
> > > --
> > > Alex Harui
> > > Flex SDK Team
> > > Adobe System, Inc.
> > > http://blogs.adobe.com/aharui
> > >
> >
> >
> >
> >
> >
> >
> > --
> > Alex Harui
> > Flex SDK Team
> > Adobe System, Inc.
> > http://blogs.adobe.com/aharui
> >
>

Reply via email to