Flex: how to control Spark datagrid header-text alignment?

ggkmath picture ggkmath · May 2, 2012 · Viewed 7.3k times · Source

If I have 10 columns in a Spark datagrid, and some headers need to be left-justified, some headers right-justified, and some centered, what's the simplest way to accomplish this?

Assuming a custom headerRenderer is needed, are there any simple examples that can walk me through it?

Thanks in advance for any comments.

Answer

ggkmath picture ggkmath · May 3, 2012

The simplest way I could find to solve this is to override the settings in the spark DefaultGridHeaderRenderer, as discussed in this link:

http://flexponential.com/2011/10/30/changing-fontweight-of-spark-datagrid-headers/

More specifically, I used the following custom headerRenderer, saved as file: myRightHeaderRenderer.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:DefaultGridHeaderRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:s="library://ns.adobe.com/flex/spark" 
            xmlns:mx="library://ns.adobe.com/flex/mx" > 

<fx:Declarations>
    <s:Label id="labelDisplay" 
             verticalCenter="1" left="0" right="0" top="0" bottom="0"
             verticalAlign="middle"
             maxDisplayedLines="1"
             textAlign="right"
             fontWeight="bold"
             showTruncationTip="true" />
</fx:Declarations>

</s:DefaultGridHeaderRenderer>

This custom header renderer right-justifies header text. To use it, simply add it to one or more columns of the Spark DataGrid as follows:

...
<fx:Array>
    <s:GridColumn ... />
    <s:GridColumn headerRenderer="myRightHeaderRenderer" ...>
    <s:GridColumn ... />
    ...
</fx:Array>
...

I'm not sure how to do it, but I'm sure it can be made more flexible by parameterizing the textAlign attribute to be center, left, or right.