November 22nd, 2007 by Kyle
Posted in: Flex
This is a follow up to my previous linebreak post.
This demonstrates how to deal with parsing linebreaks when they are in the text data for a datagrid.
You can either escape them in your source text data as shown in the previous post or you can use a labelFunction and some simple regular expression parsing to handle the escaping.
Here is the application code:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function format(item:Object, column:DataGridColumn):String{
var str:String=item[column.dataField].toString();
var myPattern:RegExp = /\\n/g;
var newStr:String=str.replace(myPattern, "\n");
return newStr;
}
]]>
</mx:Script>
<mx:Array id="arr1">
<mx:Object articleName="Finding out a characters \nUnicode character \ncode" data="15" />
<mx:Object articleName="Setting an icon in an Alert control" data="14" />
<mx:Object articleName="Setting an icon in a Button control" data="13" />
<mx:Object articleName="Installing the latest nightly \nFlex 3 SDK build into Flex Builder 3" data="10" />
<mx:Object articleName="Detecting which button a user pressed to dismiss an Alert dialog" data="9" />
<mx:Object articleName="Using the Alert control" data="8" />
<mx:Object articleName="Formatting data tips in a Slider" data="7" />
<mx:Object articleName="Downloading the latest Adobe Labs version of Flex 3 SDK/Flex Builder 3 (codename: Moxie)" data="6" />
</mx:Array>
<mx:ArrayCollection id="arrColl" source="{arr1}" />
<mx:Array id="arr2">
<mx:Object articleName="Finding out a characters {’\n‘}Unicode character code" data="15" />
<mx:Object articleName="Setting an icon in an Alert control" data="14" />
<mx:Object articleName="Setting an icon in a Button control" data="13" />
<mx:Object articleName="Installing the latest nightly {’\n‘}Flex 3 SDK build into Flex Builder 3" data="10" />
<mx:Object articleName="Detecting which button a user pressed to dismiss an Alert dialog" data="9" />
<mx:Object articleName="Using the Alert control" data="8" />
<mx:Object articleName="Formatting data tips in a Slider" data="7" />
<mx:Object articleName="Downloading the latest Adobe Labs version of Flex 3 SDK/Flex Builder 3 (codename: Moxie)" data="6" />
</mx:Array>
<mx:ArrayCollection id="arrColl2" source="{arr2}" />
<mx:Text text="this dg uses a labelFunction to manipulate the linebreaks"/>
<mx:DataGrid id="dataGrid" dataProvider="{arrColl}" variableRowHeight="true" width="60%" height="35%">
<mx:columns>
<mx:DataGridColumn labelFunction="format" dataField="articleName" headerText="Name of the article in question"/>
<mx:DataGridColumn dataField="data" headerText="ID of the article" />
</mx:columns>
</mx:DataGrid>
<mx:Text text="this dg has the linebreaks modified in the source {’\n‘}arraycollection using curly braces and single quotes."/>
<mx:DataGrid dataProvider="{arrColl2}" variableRowHeight="true" width="60%" height="35%">
<mx:columns>
<mx:DataGridColumn dataField="articleName" headerText="Name of the article in question"/>
<mx:DataGridColumn dataField="data" headerText="ID of the article" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
Browse the source of this example.
Download a zipfile containing the source to this sample.





Recent Comments