<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FlexOOP &#187; DataGrid</title>
	<atom:link href="http://flexoop.com/tag/datagrid/feed/" rel="self" type="application/rss+xml" />
	<link>http://flexoop.com</link>
	<description>Flex, AIR, ColdFusion, and everything in between</description>
	<lastBuildDate>Tue, 13 Jul 2010 03:56:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Adding colspan to a datagrid</title>
		<link>http://flexoop.com/2009/03/adding-colspan-to-a-datagrid/</link>
		<comments>http://flexoop.com/2009/03/adding-colspan-to-a-datagrid/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 11:05:51 +0000</pubDate>
		<dc:creator>Gareth</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[colspan]]></category>
		<category><![CDATA[DataGrid]]></category>

		<guid isPermaLink="false">http://flexoop.com/?p=158</guid>
		<description><![CDATA[Recently at my job, our User Interface guru came up with the design and interface that she wanted for our users. However, the design she came up with required adding a colspan to a datagrid. As many know who have tried it, this is not really a possibility right out of the box with a [...]]]></description>
			<content:encoded><![CDATA[<p>Recently at my job, our User Interface guru came up with the design and interface that she wanted for our users.  However, the design she came up with required adding a colspan to a datagrid.  As many know who have tried it, this is not really a possibility right out of the box with a Flex DataGrid.  It is apparently a possibility with the AdvancedDataGrid, but I have just never really liked the visuals of the datagrid, especially with how it outputs the rows of data&#8230;it seems very cluttered to me when grouping is added.  Plus, I have a PagedArrayCollection that I wrote and use quite regularly, that (currently) does not play nicely with the AdvancedDataGrid (I think it has something to do with the grouping), and currently just works with the regular DataGrid.  So, in order to accommodate the business and my PagedArrayCollection, rather than just have multiple rows of data in multiple columns, I came up with a solution to allow for the spanning of multiple columns, as many as is necessary.</p>
<p>I tried to figure out, visually and programmatically, I could mimic the HTML table colspan.  My solution actually involves 2 datagrids + single datagrids as itemrenderers within the rows.  As Flex recycles its itemrenderers, this appeared to be a pretty practical solution.  My first datagrid is used solely for the headers.  I made the height of the datagrid match the height of the headers of the first datagrid, so all that is visible is the headers themselves.</p>
<div class="wp-caption alignleft" style="width: 788px"><img alt="DataGrid Header" src="http://flexoop.com/wp-content/uploads/images/DataGridHeader.png" title="DataGrid Header" width="778" height="23" /><p class="wp-caption-text">DataGrid Header</p></div>
<p>The code for this is as follows (notice the small height of the datagrid):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;headerDataGrid&quot;</span> x=<span style="color: #ff0000;">&quot;70&quot;</span> y=<span style="color: #ff0000;">&quot;40&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;780&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{ dataProvider }&quot;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:columns<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn headerText=<span style="color: #ff0000;">&quot;Name&quot;</span> dataField=<span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn headerText=<span style="color: #ff0000;">&quot;Location&quot;</span> dataField=<span style="color: #ff0000;">&quot;location&quot;</span><span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn headerText=<span style="color: #ff0000;">&quot;Exp&quot;</span> dataField=<span style="color: #ff0000;">&quot;exp&quot;</span><span style="color: #66cc66;">/&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:columns<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>From here I created another datagrid, but this one contains only a single column.  I also removed the headers from this datagrid as I will be using the datagrid headers from the previously created datagrid (headerDataGrid), so this one just looks like the rows from a datagrid.</p>
<div class="wp-caption alignleft" style="width: 658px"><img alt="DataGrid Body" src="http://flexoop.com/wp-content/uploads/images/DataGridBodyMain.png" title="Main DataGrid" width="648" height="232" /><p class="wp-caption-text">DataGrid Body</p></div>
<p>Once I had these 2 datagrids, I put them right next to each other so it looks like they&#8217;re actually the same datagrid, but without vertical lines between the columns.</p>
<p>The code for the 2nd grid is:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:DataGrid x=<span style="color: #ff0000;">&quot;70&quot;</span> y=<span style="color: #ff0000;">&quot;61&quot;</span> id=<span style="color: #ff0000;">&quot;dgMain&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;780&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;444&quot;</span> showHeaders=<span style="color: #ff0000;">&quot;false&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{ dataProvider }&quot;</span> paddingTop=<span style="color: #ff0000;">&quot;0&quot;</span> paddingBottom=<span style="color: #ff0000;">&quot;0&quot;</span>
	variableRowHeight=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #0066CC;">selectable</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:columns<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn headerText=<span style="color: #ff0000;">&quot;All data&quot;</span> dataField=<span style="color: #ff0000;">&quot;col1&quot;</span> itemRenderer=<span style="color: #ff0000;">&quot;com.flexoop.utility.renderer.ColSpanRowRenderer&quot;</span> <span style="color: #66cc66;">/&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:columns<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>Both of these datagrids are bound to the same dataprovider.  This makes manipulation of the arraycollection extremely simple.  I had thought I would be writing my own sort and filter functions, overriding the headers, but I forgot that once these datagrids are bound to the same arraycollection, Flex automatically handles all of that and everything just works.  Simple! <img src='http://flexoop.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   The headertext and dataField are irrelevant in this datagrid as they will not be used.  I set the variableRowHeight=&#8221;true&#8221; so the itemrenderer will show the multiple rows of data correctly.</p>
<p>As you can see from the above code, I created a ColSpanRowRenderer itemRenderer.  This handles the final part of the colspanned datagrid&#8230;the actual colspan.</p>
<div class="wp-caption alignleft" style="width: 790px"><img alt="ColSpan Row Renderer" src="http://flexoop.com/wp-content/uploads/images/colspanrowrenderer.png" title="ColSpan Row Renderer" width="780" height="177" /><p class="wp-caption-text">ColSpan Row Renderer</p></div>
<p>The code for this looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;dgLocal&quot;</span> x=<span style="color: #ff0000;">&quot;0&quot;</span> y=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> showHeaders=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{ data }&quot;</span> backgroundAlpha=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #0066CC;">selectable</span>=<span style="color: #ff0000;">&quot;false&quot;</span>
	borderSides=<span style="color: #ff0000;">&quot;{ parentDocument.detailed ? 'bottom top' : 'bottom' }&quot;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>mx:columns<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn headerText=<span style="color: #ff0000;">&quot;Name&quot;</span> dataField=<span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn headerText=<span style="color: #ff0000;">&quot;Location&quot;</span> dataField=<span style="color: #ff0000;">&quot;location&quot;</span> <span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;</span>mx:DataGridColumn headerText=<span style="color: #ff0000;">&quot;Exp&quot;</span> dataField=<span style="color: #ff0000;">&quot;exp&quot;</span> <span style="color: #66cc66;">/&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>mx:columns<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>mx:<span style="color: #0066CC;">Text</span> <span style="color: #0066CC;">visible</span>=<span style="color: #ff0000;">&quot;{ parentDocument.detailed }&quot;</span> includeInLayout=<span style="color: #ff0000;">&quot;{ parentDocument.detailed }&quot;</span>
	<span style="color: #0066CC;">htmlText</span>=<span style="color: #ff0000;">&quot;This is where the detailed text will go.&amp;lt;br /&amp;gt;This is not formatted now but can be once it goes live&quot;</span> y=<span style="color: #ff0000;">&quot;22&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #66cc66;">/&gt;</span></pre></div></div>

<p>This is all contained within a VBox with all padding set to zero to aid in visual layout, and to make it look like it is a row in the dgMainBody datagrid.  The height of the datagrid forces it to just show the single row of data, with showHeaders=&#8221;false&#8221; again.  I bind it to the &#8220;data&#8221; value that is passed in to the itemrenderer, then output the properties of that data object within the single row datagrid.  I have a variable named &#8220;detailed&#8221; in the parentDocument (the page containing my header and main body datagrids).  This allows me to show or hide the extra colspanned row of data.  This was part of the requirement from the business, so I added it in.  As the header, main body, and colspanrowrenderers all need to be tightly coupled, I was not so worried about referring to the &#8220;parentDocument&#8221;.  Had this been something I was going to be moving around to other components, I would have tried to think of another solution.  The &#8220;includeInLayout&#8221; attribute will completely remove the &#8220;Text&#8221; box from the view, so as not to take up any space when it is not visible.  In order to prevent the user from selecting the single row datagrid item, I set &#8216;selectable=&#8221;false&#8221;&#8216;, then in the outer datagrid (dgMainBody), I set &#8216;selectable=&#8221;true&#8221;&#8216;.  This will then allow the user to select the complete itemrenderer (both datagrid row and colspanned text field).</p>
<p>I had found that when detailed=&#8217;false&#8217;, the single datagrid rows did not alternate colors, which made sense as they were all the first row.  In order to fix this problem, I set backgroundAlpha=&#8221;0&#8243; in the colspanrowrenderer.  This then used the colors of the dgMainBody datagrid for coloring the itemrenderer.  The final visual part was setting the borderSides=&#8221;bottom&#8221; or &#8220;bottom top&#8221; depending on whether the view was detailed or not.</p>
<p>The final part of getting this to look and act like a regular datagrid was to adjust the column widths of all of the ColSpanRowRenderer rows when the header items were moved, as the rows are not actually linked directly back to the dgHeader.  To catch these changes, I added a creationComplete=&#8221;init()&#8221; to the vbox in my ColSpanRowRenderer.  Then in my init() function (along with a setColumnWidth function):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
	setColumnWidth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	parentDocument.<span style="color: #006600;">headerDataGrid</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> DataGridEvent.<span style="color: #006600;">COLUMN_STRETCH</span>, setColumnWidth <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setColumnWidth<span style="color: #66cc66;">&#40;</span> event:DataGridEvent=<span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> _i:uint = <span style="color: #cc66cc;">0</span>;
	<span style="color: #000000; font-weight: bold;">var</span> _length:uint = parentDocument.<span style="color: #006600;">headerDataGrid</span>.<span style="color: #006600;">columns</span>.<span style="color: #0066CC;">length</span>;
	<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span> _i <span style="color: #66cc66;">&lt;</span> _length <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		dgLocal.<span style="color: #006600;">columns</span><span style="color: #66cc66;">&#91;</span> _i <span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">width</span> = parentDocument.<span style="color: #006600;">headerDataGrid</span>.<span style="color: #006600;">columns</span><span style="color: #66cc66;">&#91;</span> _i <span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">width</span>;
		_i++;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>At first, I was only changing the width of the column that was being stretched, but I found that the flash player must do some other computations on the other columns, so I just decided to loop over all of the columns and resize each one to match the header columns.  I found that I had to add a &#8216;render=&#8221;setColumnWidth()&#8221;&#8216; also as some of my columns were a little screwy at times.  This adds a slight adjustment after the page renders, but is not all that noticeable and fixes any column width issues I was having.</p>
<p>So the final product looks like this:</p>
<div class="wp-caption alignleft" style="width: 713px"><img alt="DataGrid With ColSpan" src="http://flexoop.com/wp-content/uploads/images/DataGridWithColSpan.png" title="DataGrid With ColSpan" width="703" height="319" /><p class="wp-caption-text">DataGrid With ColSpan</p></div>
<p>And code is <a href="/wp-content/uploads/code/DataGridColSpan/DataGridColSpan.mxml" target="_blank">DataGridColSpan.mxml</a> and <a href="/wp-content/uploads/code/DataGridColSpan/ColSpanRowRenderer.mxml" target="_blank">ColSpanRowRenderer.mxml</a></p>
<p>Hopefully I haven&#8217;t bored anyone to tears with my explanation, but I wanted to be sure that everyone understands my reasoning for doing everything that I did to the datagrids.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://flexoop.com/2009/03/adding-colspan-to-a-datagrid/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
