Toggling word wrap on a DataGrid column header in Flex
The following example shows how you can toggle word wrapping on a Flex DataGrid control’s DataGridColumn by setting the headerWordWrap property.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/14/toggling-word-wrap-on-a-datagrid-column-header-in-flex/ -->
<mx:Application name="DataGridColumn_headerWordWrap_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white">
<mx:XML id="dp" source="data/products.xml" />
<mx:ApplicationControlBar dock="true">
<mx:Form styleName="plain">
<mx:FormItem label="headerWordWrap:">
<mx:CheckBox id="checkBox" />
</mx:FormItem>
<mx:FormItem label="rowCount:">
<mx:HSlider id="slider"
minimum="2"
maximum="10"
value="6"
snapInterval="1"
tickInterval="1"
liveDragging="true"
showTrackHighlight="true" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<mx:DataGrid id="dataGrid"
dataProvider="{dp.product}"
rowCount="{slider.value}"
verticalScrollPolicy="on"
width="300">
<mx:columns>
<mx:DataGridColumn id="dataGridColumn1"
dataField="@name"
headerText="This is a column with a long title:"
headerWordWrap="{checkBox.selected}"
minWidth="80" />
<mx:DataGridColumn id="dataGridColumn2"
dataField="@price"
headerText="Price:"
headerWordWrap="false"
minWidth="20" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
And the XML file, data/products.xml, is as follows:
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/09/14/toggling-word-wrap-on-a-datagrid-column-header-in-flex/ --> <products> <product name="Product 1" price="1.99" /> <product name="Product 2" price="2.99" /> <product name="Product 3" price="3.99" /> <product name="Product 4" price="4.99" /> <product name="Product 5" price="5.99" /> <product name="Product 6" price="6.99" /> <product name="Product 7" price="7.99" /> <product name="Product 8" price="8.99" /> <product name="Product 9" price="9.99" /> <product name="Product 0" price="0.99" /> </products>
View source is enabled in the following example.
You can also set the headerWordWrap property using ActionScript, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/14/toggling-word-wrap-on-a-datagrid-column-header-in-flex/ -->
<mx:Application name="DataGridColumn_headerWordWrap_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white">
<mx:Script>
<![CDATA[
private function checkBox_change(evt:Event):void {
dataGridColumn1.headerWordWrap = checkBox.selected;
}
]]>
</mx:Script>
<mx:XML id="dp" source="data/products.xml" />
<mx:ApplicationControlBar dock="true">
<mx:Form styleName="plain">
<mx:FormItem label="headerWordWrap:">
<mx:CheckBox id="checkBox"
change="checkBox_change(event);" />
</mx:FormItem>
<mx:FormItem label="rowCount:">
<mx:HSlider id="slider"
minimum="2"
maximum="10"
value="6"
snapInterval="1"
tickInterval="1"
liveDragging="true"
showTrackHighlight="true" />
</mx:FormItem>
</mx:Form>
</mx:ApplicationControlBar>
<mx:DataGrid id="dataGrid"
dataProvider="{dp.product}"
rowCount="{slider.value}"
verticalScrollPolicy="on"
width="300">
<mx:columns>
<mx:DataGridColumn id="dataGridColumn1"
dataField="@name"
headerText="This is a column with a long title:"
minWidth="80" />
<mx:DataGridColumn id="dataGridColumn2"
dataField="@price"
headerText="Price:"
headerWordWrap="false"
minWidth="20" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
Due to popular demand, here is the “same” example in a more ActionScript friendly format:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/14/toggling-word-wrap-on-a-datagrid-column-header-in-flex/ -->
<mx:Application name="DataGridColumn_headerWordWrap_test"
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="top"
backgroundColor="white"
initialize="init();">
<mx:Script>
<![CDATA[
import mx.containers.ApplicationControlBar;
import mx.containers.Form;
import mx.containers.FormItem;
import mx.controls.CheckBox;
import mx.controls.DataGrid;
import mx.controls.HSlider;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.ScrollPolicy;
import mx.events.SliderEvent;
private var checkBox:CheckBox;
private var slider:HSlider;
private var dataGrid:DataGrid;
private var dataGridColumn1:DataGridColumn;
private var dataGridColumn2:DataGridColumn;
private function init():void {
checkBox = new CheckBox();
checkBox.addEventListener(Event.CHANGE,
checkBox_change);
slider = new HSlider();
slider.minimum = 2;
slider.maximum = 10;
slider.value = 6;
slider.snapInterval = 1;
slider.tickInterval = 1;
slider.liveDragging = true;
slider.setStyle("showTrackHighlight", true);
slider.addEventListener(SliderEvent.CHANGE,
slider_change);
var formItem1:FormItem = new FormItem();
formItem1.label = "headerWordWrap:";
formItem1.addChild(checkBox);
var formItem2:FormItem = new FormItem();
formItem2.label = "rowCount:";
formItem2.addChild(slider);
var form:Form = new Form();
form.styleName = "plain";
form.addChild(formItem1);
form.addChild(formItem2);
var appControlBar:ApplicationControlBar;
appControlBar = new ApplicationControlBar();
appControlBar.dock = true;
appControlBar.addChild(form);
addChildAt(appControlBar, 0);
dataGridColumn1 = new DataGridColumn();
dataGridColumn1.dataField = "@name";
dataGridColumn1.headerText = "This is a column with a long title:";
dataGridColumn1.minWidth = 80;
dataGridColumn2 = new DataGridColumn();
dataGridColumn2.dataField = "@price";
dataGridColumn2.headerText = "Price:";
dataGridColumn2.headerWordWrap = false;
dataGridColumn2.minWidth = 20;
dataGrid = new DataGrid();
dataGrid.columns = [dataGridColumn1, dataGridColumn2];
dataGrid.dataProvider = dp.product;
dataGrid.rowCount = 6;
dataGrid.verticalScrollPolicy = ScrollPolicy.ON;
dataGrid.width = 300;
addChild(dataGrid);
}
private function checkBox_change(evt:Event):void {
dataGridColumn1.headerWordWrap = checkBox.selected;
}
private function slider_change(evt:SliderEvent):void {
dataGrid.rowCount = evt.value;
}
]]>
</mx:Script>
<mx:XML id="dp" source="data/products.xml" />
</mx:Application>
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
9 Responses to Toggling word wrap on a DataGrid column header in Flex
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Just what I needed. Thanks.
Now, how do I center the header text?
Jeff,
Try this:
<mx:Style> DataGrid { headerStyleName: centerAlignedBold; } .centerAlignedBold { fontWeight: bold; textAlign: center; } </mx:Style>Peter
And just because I love… “Aligning the header text in a DataGrid column in Flex”
Peter
HI,
how to insert button within the columns of data grid.
thanks a lot..
Akhil
Seems if you try and style the header with CSS fontSize or fontFamily the header text doesn’t wrap. However, if I set the headerHeight high enough then it does. Is this a bug?
@Dan G,
I’m not sure if it’s a bug. Can you file a bug at http://bugs.adobe.com/flex/ and include your test case and somebody more familiar with the DataGrid control can investigate.
Thanks,
Peter
I have create a headerRenderer with a TextInput for a DataGridColumn in datagrid component.. But when i am doing filtering through the textinput change then i am getting filter data based on last type letter only …. and also i can not see the letter what i have typed in textinput … can anyone plzzzz help me in this aspect???
Hi,
In case you have a preceding hidden column (visible = “false”), then the headerWordWrap does not work any longer.
Do you have an idea for this behaviour??
Thanks.
Best regards,
Guillaume
Oops..Sorry… seems to be an unresolved bug
http://bugs.adobe.com/jira/browse/SDK-29963