<?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>Li Guoliang &#187; DataGrid</title>
	<atom:link href="http://liguoliang.com/tag/datagrid/feed/" rel="self" type="application/rss+xml" />
	<link>http://liguoliang.com</link>
	<description>ActionScript Flex Java JEE PHP...</description>
	<lastBuildDate>Mon, 21 May 2012 17:04:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to display errorString in a Datagrid ItemEditor?</title>
		<link>http://liguoliang.com/2011/how-to-display-errorstring-in-a-datagrid-itemeditor/</link>
		<comments>http://liguoliang.com/2011/how-to-display-errorstring-in-a-datagrid-itemeditor/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 14:38:42 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[itemEditor]]></category>

		<guid isPermaLink="false">http://liguoliang.com/?p=2205</guid>
		<description><![CDATA[There is a datagrid displaying users:
<a href="http://liguoliang.com/wp-content/uploads/2011/12/image1.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://liguoliang.com/wp-content/uploads/2011/12/image_thumb1.png" width="320" height="185"/></a>
And this datagrid is editable ,&#160; we can modify username directly, by default, Flex will use TextInput as t<p class='read-more'><a href='http://liguoliang.com/2011/how-to-display-errorstring-in-a-datagrid-itemeditor/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>There is a datagrid displaying users:</p>
<p><a href="http://liguoliang.com/wp-content/uploads/2011/12/image1.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://liguoliang.com/wp-content/uploads/2011/12/image_thumb1.png" width="320" height="185"></a></p>
<p>And this datagrid is editable ,&nbsp; we can modify username directly, by default, Flex will use TextInput as the item editor.</p>
<p>I want to display some error mesg in a datagrid ItemEditor when use input is invalidate.</p>
<h4>Using Alert – not good enough</h4>
<p>So I add a listener to the item edit complete Event:</p>
<pre class="java" name="code">	/**
	 * validate user input, revert the original value if necessary.
	 */
	protected function datagirdTest_itemEditEndHandler(event:DataGridEvent):void {
		if(event.dataField != "Name") { // chech the field;
			return;
		}
		var input:String = (datagirdTest.itemEditorInstance as TextInput).text; // get the user input data.
		if(input == null || input == "") {
			event.preventDefault(); // prevent default behavior
			(datagirdTest.itemEditorInstance as TextInput).text = (event.itemRenderer.data as XML).Name;// Undo: revert the original data by the selected item.
			Alert.show(errorMesg);
			return;
		}
	}
</pre>
<p>It can work correctly, but our client think we should not change user input directly. so I comment off the undo line: </p>
<pre class="java" name="code">	/**
	 * validate user input, revert the original value if necessary.
	 */
	protected function datagirdTest_itemEditEndHandler(event:DataGridEvent):void {
		if(event.dataField != "Name") { // chech the field;
			return;
		}
		var input:String = (datagirdTest.itemEditorInstance as TextInput).text; // get the user input data.
		if(input == null || input == "") {
			event.preventDefault(); // prevent default behavior
			//HERE: (datagirdTest.itemEditorInstance as TextInput).text = (event.itemRenderer.data as XML).Name;// Undo: revert the original data by the selected item.
			Alert.show(errorMesg);
			return;
		}
	}
</pre>
<p>Unfortunately, because of ‘Alert’, itemEditComplete Event will dispatch many times, so there’ll be many alert in the UI. </p>
<p>&nbsp;</p>
<h4>Using Error message in item editor – can not work correctly</h4>
<p>So, I want to display the error message in the item editor , like this: </p>
<p><a href="http://liguoliang.com/wp-content/uploads/2011/12/image2.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://liguoliang.com/wp-content/uploads/2011/12/image_thumb2.png" width="399" height="189"></a></p>
<p>and here is the handler: </p>
<pre class="java" name="code">	/**
	 * validate user input.
	 */
	protected function datagirdTest_itemEditEndHandler(event:DataGridEvent):void {
		if(event.dataField != "Name") { // check the field;
			return;
		}

		var editor:TextInput = datagirdTest.itemEditorInstance as TextInput;
		var input:String = editor.text; // get the user input data.
		if(input == null || input == "") {
			// event.preventDefault(); // prevent default behavior
			event.stopImmediatePropagation();
			editor.errorString = "User name cannot be empty!";
			trace("Error");
			// datagirdTest.destroyItemEditor();
		}else {
			editor.errorString = null;
		}
	}
</pre>
<p>Now I can display the error mesg as the above screen capture, but if the user want to reload the data or sort the column, the editor will stay there until I call datagrid.destroyItemEditor(), but:</p>
<p>“This method closes an item editor currently open on an item renderer. You typically only call this method from within the event listener for the <code>itemEditEnd</code> event, after you have already called the <code>preventDefault()</code> method to prevent the default event listener from executing.” <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/DataGrid.html#destroyItemEditor()">http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/DataGrid.html#destroyItemEditor()</a></p>
<p><strong>so, How can I display the error message in this way?</strong></p>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2011/how-to-display-errorstring-in-a-datagrid-itemeditor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flex: 检查/撤销Datagrid编辑数据 Validate/revert editable Datagrid input value</title>
		<link>http://liguoliang.com/2011/validaterevert-editable-datagrid-input-value/</link>
		<comments>http://liguoliang.com/2011/validaterevert-editable-datagrid-input-value/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 13:42:11 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[itemEditor]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2011/flex-%e6%a3%80%e6%9f%a5%e6%92%a4%e9%94%80datagrid%e7%bc%96%e8%be%91%e6%95%b0%e6%8d%ae-validaterevert-editable-datagrid-input-value/</guid>
		<description><![CDATA[Requirement: We want to validate user input in editable datagrid, and revert the original value(undo) if necessary.
Solution: Handle the ‘itemEditEnd’<p class='read-more'><a href='http://liguoliang.com/2011/validaterevert-editable-datagrid-input-value/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>Requirement: We want to validate user input in editable datagrid, and revert the original value(undo) if necessary.</p>
<p>Solution: Handle the ‘itemEditEnd’ Event dispatched by the datagrid.</p>
<p>Codes:</p>
<pre class="java" name="code">	/**
	 * validate user input, revert the original value if necessary.
	 */
	protected function datagirdTest_itemEditEndHandler(event:DataGridEvent):void {
		if(event.dataField != "Name") { // chech the field;
			return;
		}
		var input:String = (datagirdTest.itemEditorInstance as TextInput).text; // get the user input data.
		if(input == null || input == "") {
			event.preventDefault(); // prevent default behavior
			// var filed:String = (datagirdTest.columns[event.columnIndex] as DataGridColumn).editorDataField;
			// trace(datagirdTest.itemEditorInstance[filed]);
			(datagirdTest.itemEditorInstance as TextInput).text = (event.itemRenderer.data as XML).Name;// Undo: revert the original data by the selected item.
			Alert.show(errorMesg);
			return;
		}
	}
</pre>
<p>Screen caputre: </p>
</p>
<p><a href="http://liguoliang.com/wp-content/uploads/2011/12/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://liguoliang.com/wp-content/uploads/2011/12/image_thumb.png" width="512" height="406"></a> </p>
<p>User input is empty, we got the Event, prevent the default behavior, and revert the value form the modeling.</p>
<p>Ref:</p>
<p>1. itemEditEnd called multiple times <a title="http://forums.adobe.com/message/2459209" href="http://forums.adobe.com/message/2459209">http://forums.adobe.com/message/2459209</a><br />2. Using cell editing events&nbsp; &#8211; <a title="http://livedocs.adobe.com/flex/3/html/help.html?content=celleditor_7.html" href="http://livedocs.adobe.com/flex/3/html/help.html?content=celleditor_7.html">http://livedocs.adobe.com/flex/3/html/help.html?content=celleditor_7.html</a></p>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2011/validaterevert-editable-datagrid-input-value/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flex: Setting Datagrid column percentWidth &#8211; 设置Datagrid 列宽度为百分比</title>
		<link>http://liguoliang.com/2010/flex-setting-datagrid-column-percentwidth-%e8%ae%be%e7%bd%aedatagrid-%e5%88%97%e5%ae%bd%e5%ba%a6%e4%b8%ba%e7%99%be%e5%88%86%e6%af%94/</link>
		<comments>http://liguoliang.com/2010/flex-setting-datagrid-column-percentwidth-%e8%ae%be%e7%bd%aedatagrid-%e5%88%97%e5%ae%bd%e5%ba%a6%e4%b8%ba%e7%99%be%e5%88%86%e6%af%94/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 14:31:18 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[DataGrid percentWidth]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2010/08/1447/</guid>
		<description><![CDATA[<p><strong>需求</strong>: 设置DatagridColumn的Percent width</p>
<p><strong>临时解决方法</strong>: 监听Datagrid的CREATION_COMPLETE事件, 在响应函数中重置Column的宽度.</p>
<p><strong>效果</strong>: 参见<a href="http://cutown.com/home/reg.php">http://cutown.com/home/reg.php</a>, 网页内嵌入的Datagrid中, 后两列宽度为首列之外的宽度均分.</p>
<p> </p><p class='read-more'><a href='http://liguoliang.com/2010/flex-setting-datagrid-column-percentwidth-%e8%ae%be%e7%bd%aedatagrid-%e5%88%97%e5%ae%bd%e5%ba%a6%e4%b8%ba%e7%99%be%e5%88%86%e6%af%94/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p><strong>需求</strong>: 设置DatagridColumn的Percent width</p>
<p><strong>临时解决方法</strong>: 监听Datagrid的CREATION_COMPLETE事件, 在响应函数中重置Column的宽度.</p>
<p><strong>效果</strong>: 参见<a href="http://cutown.com/home/reg.php">http://cutown.com/home/reg.php</a>, 网页内嵌入的Datagrid中, 后两列宽度为首列之外的宽度均分.</p>
<p> <span id="more-1447"></span>
<p><strong>代码</strong>:</p>
<pre class="java" name="code">	/** Datagrid创建完毕后设定Column的宽度. */
	protected function onDGCreationComplte(e:Event):void {
		var columnServerID:DataGridColumn = columns[0] as DataGridColumn;

		for(var i:int = 0; i &lt; columns.length; i++) {
			var column:DataGridColumn = columns[i] as DataGridColumn;
			column.width = width/columns.length; // 重设宽度, 减去首列宽度, 其他列均分.
		}
	}</pre>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2010/flex-setting-datagrid-column-percentwidth-%e8%ae%be%e7%bd%aedatagrid-%e5%88%97%e5%ae%bd%e5%ba%a6%e4%b8%ba%e7%99%be%e5%88%86%e6%af%94/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flex: DataGrid使用ListEvent.ITEM_DOUBLE_CLICK监听双击事件</title>
		<link>http://liguoliang.com/2010/flex-datagrid%e4%bd%bf%e7%94%a8listevent-item_double_click%e7%9b%91%e5%90%ac%e5%8f%8c%e5%87%bb%e4%ba%8b%e4%bb%b6/</link>
		<comments>http://liguoliang.com/2010/flex-datagrid%e4%bd%bf%e7%94%a8listevent-item_double_click%e7%9b%91%e5%90%ac%e5%8f%8c%e5%87%bb%e4%ba%8b%e4%bb%b6/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 08:48:19 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[DoubleClicked]]></category>
		<category><![CDATA[ListEvent]]></category>
		<category><![CDATA[MouseEvent]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2010/01/1133/</guid>
		<description><![CDATA[之前没怎么注意, 一直用MouseEvent.DOUBLE_CLICK来监听DataGrid的双击事件. 但这样会造成即使在空行双击也会激发响应.
		datagridClasses.doubleClickEnabled = true;
		datagridClasses.addEventListe<p class='read-more'><a href='http://liguoliang.com/2010/flex-datagrid%e4%bd%bf%e7%94%a8listevent-item_double_click%e7%9b%91%e5%90%ac%e5%8f%8c%e5%87%bb%e4%ba%8b%e4%bb%b6/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>之前没怎么注意, 一直用MouseEvent.DOUBLE_CLICK来监听DataGrid的双击事件. 但这样会造成即使在空行双击也会激发响应.</p>
<pre class="java" name="code">		datagridClasses.doubleClickEnabled = true;
		datagridClasses.addEventListener(MouseEvent.DOUBLE_CLICK, onDGDoubleClicked);</pre>
<p>正确方法应该使用ListEvent.ITEM_DOUBLE_CLICK监听项目双击事件.</p>
<pre class="java" name="code">		datagridClasses.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onDGDoubleClicked);</pre>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2010/flex-datagrid%e4%bd%bf%e7%94%a8listevent-item_double_click%e7%9b%91%e5%90%ac%e5%8f%8c%e5%87%bb%e4%ba%8b%e4%bb%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flex: Datagrid派发ITEM_EDIT_BEGIN后因fm/focusManager为Null报错的解决方法</title>
		<link>http://liguoliang.com/2010/flex-datagrid%e6%b4%be%e5%8f%91item_edit_begin%e5%90%8e%e5%9b%a0fmfocusmanager%e4%b8%banull%e6%8a%a5%e9%94%99%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/</link>
		<comments>http://liguoliang.com/2010/flex-datagrid%e6%b4%be%e5%8f%91item_edit_begin%e5%90%8e%e5%9b%a0fmfocusmanager%e4%b8%banull%e6%8a%a5%e9%94%99%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 14:09:59 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[focusManager]]></category>
		<category><![CDATA[ITEM_EDIT_BEGIN]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2010/01/1110/</guid>
		<description><![CDATA[某DataGrid 监听Editor_BEGIN事件, 如果有数据未保存切换界面时, 会弹出Alert, 在弹出Alert之后, 确定切换界面时, DataGrid报错: DataGrid的itemEditorItemEditBeginHandler方法中: var fm:IFocusManager<p class='read-more'><a href='http://liguoliang.com/2010/flex-datagrid%e6%b4%be%e5%8f%91item_edit_begin%e5%90%8e%e5%9b%a0fmfocusmanager%e4%b8%banull%e6%8a%a5%e9%94%99%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>某DataGrid 监听Editor_BEGIN事件, 如果有数据未保存切换界面时, 会弹出Alert, 在弹出Alert之后, 确定切换界面时, DataGrid报错:</br> DataGrid的itemEditorItemEditBeginHandler方法中:</br> var fm:IFocusManager = focusManager; 该fm也就是focusManager为Null, 导致出现错误.   </br> 恰好focusManager属性为Protected.</br> 解决方法, 在DataGridGrade(该类继承了DataGrid)的onEditBegin方法中增加了
<pre name="code" class="java">
if(focusManager == null) {
	e.preventDefault()
	return;
}
</pre>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2010/flex-datagrid%e6%b4%be%e5%8f%91item_edit_begin%e5%90%8e%e5%9b%a0fmfocusmanager%e4%b8%banull%e6%8a%a5%e9%94%99%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>使用ActionScript建立DataGrid, 添加右键, 增加列, 并设定列的ItemRenderer</title>
		<link>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8actionscript%e5%bb%ba%e7%ab%8bdatagrid-%e6%b7%bb%e5%8a%a0%e5%8f%b3%e9%94%ae-%e5%a2%9e%e5%8a%a0%e5%88%97-%e5%b9%b6%e8%ae%be%e5%ae%9a%e5%88%97%e7%9a%84itemrender/</link>
		<comments>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8actionscript%e5%bb%ba%e7%ab%8bdatagrid-%e6%b7%bb%e5%8a%a0%e5%8f%b3%e9%94%ae-%e5%a2%9e%e5%8a%a0%e5%88%97-%e5%b9%b6%e8%ae%be%e5%ae%9a%e5%88%97%e7%9a%84itemrender/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 16:59:39 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Air右键]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[DataGridColumn]]></category>
		<category><![CDATA[Flex右键]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2009/01/679/</guid>
		<description><![CDATA[很多时候为了获得对组件的灵活控制, 不得不放弃MXML, 直接使用ActionScript.<p class='read-more'><a href='http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8actionscript%e5%bb%ba%e7%ab%8bdatagrid-%e6%b7%bb%e5%8a%a0%e5%8f%b3%e9%94%ae-%e5%a2%9e%e5%8a%a0%e5%88%97-%e5%b9%b6%e8%ae%be%e5%ae%9a%e5%88%97%e7%9a%84itemrender/'></a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>很多时候为了获得对组件的灵活控制, 不得不放弃MXML, 直接使用ActionScript.
<pre class="java" name="code">
//新建一个DataGrid _dataGrid = new DataGrid();
//增加右键
var menu:NativeMenu = new NativeMenu();
var menuItemDelete:NativeMenuItem = new NativeMenuItem(&quot;Delete&quot;);
//监听事件,在右键选定Delete时运行该函数
 menuItemDelete.addEventListener(Event.SELECT, onMenuDeleteClicked);
//将该menuItem加入到Menu中
menu.addItem(menuItemDelete);
 _dataGrid.contextMenu = menu; //将菜单加入到DataGrid中 

columnLocale = new DataGridColumn(); //新建一个列
columnLocale.dataField ="locale";//设定DataGrid中locale列的EditItem;
comboBoxLocaleEditor = new ClassFactory(ComboBox); // D
comboBoxLocaleEditor.properties = {dataProvider : LocalizationItem.localeArray} //设定该EditorItem的属性
columnLocale.itemEditor = comboBoxLocaleEditor; 

var columnGender:DataGridColumn = new DataGridColumn();
 columnGender.headerText = "Gender&#038;quot";
columnGender.dataField = "Gender_";
//使用ItemRender
columnGender.itemRenderer = new ClassFactory(Gender_Label);
 _dataGrid.showHeaders = false; //隐藏DataGrid的Header
_dataGrid.columns = [columnLocale, columnGender]; //这一个数组,存放DataGrid的Column 

_dataGrid.dataProvider = value; //设定dataProvider </pre>
<p>具体在实际中的使用, 无异于MXML. </p>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8actionscript%e5%bb%ba%e7%ab%8bdatagrid-%e6%b7%bb%e5%8a%a0%e5%8f%b3%e9%94%ae-%e5%a2%9e%e5%8a%a0%e5%88%97-%e5%b9%b6%e8%ae%be%e5%ae%9a%e5%88%97%e7%9a%84itemrender/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>使用ItemRenderer处理基于List容器的显示内容 [DataGrid, Tree, List等]</title>
		<link>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8itemrender%e5%a4%84%e7%90%86%e5%9f%ba%e4%ba%8elist%e5%ae%b9%e5%99%a8%e7%9a%84%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9-datagrid-tree-list%e7%ad%89/</link>
		<comments>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8itemrender%e5%a4%84%e7%90%86%e5%9f%ba%e4%ba%8elist%e5%ae%b9%e5%99%a8%e7%9a%84%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9-datagrid-tree-list%e7%ad%89/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 16:52:06 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[DataGridColumn]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2009/01/678/</guid>
		<description><![CDATA[如Student中有一Gender_属性, 1表示男生, 2表示女生, 3表示未知, 现在有下要求: 1. 在DataGrid中不可以显示1, 2,3, 应显示 男女 [ 该功能可由LabelFunction实现], 2. 如果为男生,, 则”男生”颜色为红色, 女生颜色为默认, 未知字体大小为10号<p class='read-more'><a href='http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8itemrender%e5%a4%84%e7%90%86%e5%9f%ba%e4%ba%8elist%e5%ae%b9%e5%99%a8%e7%9a%84%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9-datagrid-tree-list%e7%ad%89/'></a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p><strong>具体的情况</strong>如:<a href="http://liguoliang.com/2009/01/668/">使用labelFunction处理DataGrid显示内容 </a>, 只是这里使用的是ItemRender来实现并实现更多功能.</p>
<p>如Student中有一Gender_属性, 1表示男生, 2表示女生, 3表示未知, 现在有下要求:</p>
<p>1. 在DataGrid中不可以显示1, 2,3, 应显示 男女 [ 该功能可由LabelFunction实现],</p>
<p>2. 如果为男生,, 则”男生”颜色为红色, 女生颜色为默认, 未知字体大小为10号</p>
<p><strong>具体实现:</strong></p>
<pre class="java" name="code">			var columnGender:DataGridColumn = new DataGridColumn();
			columnGender.headerText = &quot;Gender&quot;;
			columnGender.dataField = &quot;Gender_&quot;; //使用ItemRender
			columnGender.itemRenderer =  new ClassFactory(Gender_Label);
			//columnGender.labelFunction = formatGender;

			_dataGridStudent.columns = [columnID, columnName, columnAge, columnGender];</pre>
<p>ItemRender实现: 该类将继承List, 重写其UpdateDisplayList函数, 如下: </p>
<pre class="java" name="code">package com.test.itemRender
{
import mx.controls.Label;
import mx.controls.listClasses.ListBase;

public class Gender_Label extends Label
{
	/**
	 * 构造函数
	 */
	public function Gender_Label() {
		super();
	}

	override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
		super.updateDisplayList(unscaledWidth, unscaledHeight);
		if(data.Gender_ == 1) {
             setStyle('fontSize',14);
             setStyle('fontWeight', 'bold');
             setStyle('color', 0xDD0000);
             this.text = &quot;男生&quot;;
		 }else if(data.Gender_ == 2) {
		 	setStyle('fontSize',14);
		 	this.text = &quot;女生&quot;
		 }
		 //otherwise, return the label to its regular state
		 else {
		     setStyle('fontSize', 10);
             this.text = &quot;未知&quot;;
		 }
	}

}// end of class
}// end of package</pre>
<p><strong>最终效果:</strong> </p>
<p><a href="http://liguoliang.com/wp-content/uploads/2009/01/image3.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="282" alt="image" src="http://liguoliang.com/wp-content/uploads/2009/01/image-thumb3.png" width="344" border="0" /></a></p>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8itemrender%e5%a4%84%e7%90%86%e5%9f%ba%e4%ba%8elist%e5%ae%b9%e5%99%a8%e7%9a%84%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9-datagrid-tree-list%e7%ad%89/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AIR中通过右键直接选定基于LIST容器[DataGrid, List, Tree等]的数据 &#8211; Select List item with mouse right-click</title>
		<link>http://liguoliang.com/2009/air%e4%b8%ad%e9%80%9a%e8%bf%87%e5%8f%b3%e9%94%ae%e7%9b%b4%e6%8e%a5%e9%80%89%e5%ae%9a%e5%9f%ba%e4%ba%8elist%e5%ae%b9%e5%99%a8datagrid-list-tree%e7%ad%89%e7%9a%84%e6%95%b0%e6%8d%ae-select-list-ite/</link>
		<comments>http://liguoliang.com/2009/air%e4%b8%ad%e9%80%9a%e8%bf%87%e5%8f%b3%e9%94%ae%e7%9b%b4%e6%8e%a5%e9%80%89%e5%ae%9a%e5%9f%ba%e4%ba%8elist%e5%ae%b9%e5%99%a8datagrid-list-tree%e7%ad%89%e7%9a%84%e6%95%b0%e6%8d%ae-select-list-ite/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 15:18:16 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Air右键]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[Flex右键]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[Tree]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2009/01/675/</guid>
		<description><![CDATA[在很多情况下, 我们在DataGrid, List, Tree等容器中使用右键, 进行如修改, 删除 某行的操作.  问题是如果该容器初始状态下直接进行右键点击时, 并不能选定任何数据. 通过监听右键菜单SELECT事件, 获取到当前右键所击位置的Index, 并将之赋值给DataGrid或其他容器的SelectIndex, 完成点击操作<p class='read-more'><a href='http://liguoliang.com/2009/air%e4%b8%ad%e9%80%9a%e8%bf%87%e5%8f%b3%e9%94%ae%e7%9b%b4%e6%8e%a5%e9%80%89%e5%ae%9a%e5%9f%ba%e4%ba%8elist%e5%ae%b9%e5%99%a8datagrid-list-tree%e7%ad%89%e7%9a%84%e6%95%b0%e6%8d%ae-select-list-ite/'></a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p><strong>问题:</strong> 在很多情况下, 我们在DataGrid, List, Tree等容器中使用右键, 进行如修改, 删除 某行的操作.&#160; 问题是如果该容器初始状态下直接进行右键点击时, 并不能选定任何数据. </p>
<p><strong>解决: </strong>通过监听右键菜单SELECT事件, 获取到当前右键所击位置的Index, 并将之赋值给DataGrid或其他容器的SelectIndex, 完成点击操作</p>
<p><strong>详细实现:&#160; </strong>以DataGrid为例.</p>
<p>第一步, 首先给DataGrid添加菜单</p>
<pre class="java" name="code">			var dgMenu:NativeMenu = new NativeMenu();
			var del:NativeMenuItem  = new NativeMenuItem(&quot;Test&quot;);
			del.addEventListener(Event.SELECT, onRightClickDel);
			dgMenu.addItem(del);
			dgMenu.addEventListener(ContextMenuEvent.MENU_SELECT, onRightClicked);
			_dataGridStudent.contextMenu = dgMenu;</pre>
<p>第二步, 响应函数:</p>
<pre class="java" name="code">	/**响应右键点击 */
	private function onRightClicked(e:ContextMenuEvent):void {
		var rightClickItemRender:IListItemRenderer;

		if(e.mouseTarget is IListItemRenderer) {
			rightClickItemRender = IListItemRenderer(e.mouseTarget);
		}else if(e.mouseTarget.parent is IListItemRenderer) {
			rightClickItemRender = IListItemRenderer(e.mouseTarget.parent);
		}

		if(rightClickItemRender != null) {
			var rightClickIndex:int = _dataGridStudent.itemRendererToIndex(rightClickItemRender);
			if(_dataGridStudent.selectedIndex != rightClickIndex) {
				_dataGridStudent.selectedIndex = rightClickIndex;
				onSlectedChange();
			}
		}
		trace(&quot;通过右键单击获得选定的行: &quot; + _dataGridStudent.selectedIndex);
	}</pre>
<p>Delete响应函数:</p>
<pre class="java" name="code">	/** 当右键菜单的Delete点击后响应*/
	private function onRightClickDel(e:Event):void {
		//在这里放逻辑....
	}</pre>
<p><strong>效果:</strong></p>
<p><a href="http://liguoliang.com/wp-content/uploads/2009/01/image2.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="313" alt="image" src="http://liguoliang.com/wp-content/uploads/2009/01/image-thumb2.png" width="390" border="0" /></a> </p>
<p>控制台Trace信息:</p>
<p><em>通过右键单击获得选定的行: 1</em></p>
</p>
<p>&#160;</p>
<p>参考资料: <a href="http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&amp;postId=11606&amp;productId=2&amp;loc=en_US" target="_blank">Adobe CookBook&#160; Select List item with mouse right-click</a></p>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2009/air%e4%b8%ad%e9%80%9a%e8%bf%87%e5%8f%b3%e9%94%ae%e7%9b%b4%e6%8e%a5%e9%80%89%e5%ae%9a%e5%9f%ba%e4%ba%8elist%e5%ae%b9%e5%99%a8datagrid-list-tree%e7%ad%89%e7%9a%84%e6%95%b0%e6%8d%ae-select-list-ite/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>使用labelFunction处理DataGrid显示内容</title>
		<link>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8labelfunction%e5%a4%84%e7%90%86datagrid%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9/</link>
		<comments>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8labelfunction%e5%a4%84%e7%90%86datagrid%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 09:31:44 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[DataGridColumn]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2009/01/668/</guid>
		<description><![CDATA[在设定了DataGrid的DataProvider, 设定DataGridColumn及其相应的DataField之后, DataGrid就可正常显示, 但有时候需要稍微处理一下显示内容: 如 某ArrayCollection中含有一组Student, Student对象具有gender属性, 其中1代表男, 2代表女, 3代表不确定. 此时需要使用labelFunction进行处理显示内容.<p class='read-more'><a href='http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8labelfunction%e5%a4%84%e7%90%86datagrid%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9/'></a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>在设定了DataGrid的DataProvider, 设定DataGridColumn及其相应的DataField之后, DataGrid就可正常显示, 但有时候需要稍微处理一下显示内容: 如 某ArrayCollection中含有一组Student, Student对象具有gender属性, 其中1代表男, 2代表女, 3代表不确定. 此时需要使用labelFunction进行处理显示内容.</p>
<p><a href="http://liguoliang.com/wp-content/uploads/2009/01/image.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="image" src="http://liguoliang.com/wp-content/uploads/2009/01/image-thumb.png" border="0" alt="image" width="445" height="344" /></a> <a href="http://liguoliang.com/wp-content/uploads/2009/01/image1.png"><img style="border: 0pt none; display: inline;" title="image" src="http://liguoliang.com/wp-content/uploads/2009/01/image1.png" border="0" alt="image" width="437" height="344" /></a></p>
<p><strong>原理:</strong></p>
<p>DataGrid通过DataGridColum函数中的itemToLabel实现数据源到现实内容的过程,</p>
<p>当DataGrid 某列的labelFunction不为空时, DataGridColumn的itemToLabel函数将执行:</p>
<p>if (labelFunction != null)<br />
return labelFunction(data, this);</p>
<p>具体可参见类DataGridColumn的itemToLabel函数.</p>
<p><strong>使用方法:</strong></p>
<pre class="java">var columnGender:DataGridColumn = new DataGridColumn();  //增加一个Column
			columnGender.headerText = "Gender";
			columnGender.dataField = "Gender_";
			columnGender.labelFunction = formatGender;  //设定LabelFunction

			_dataGridStudent.columns = [columnID, columnName, columnAge, columnGender];  //将Column加入到DG中.总共四个Column</pre>
<p>labelFunction函数</p>
<pre class="java">	/** 设定DataGrid 性别栏显示内容*/
	 private function formatGender(item:Object, c:DataGridColumn):String {
	 	var gender:String;
	 	if(item.Gender_ == 1) {
	 		gender = "男";
	 	}else if(item.Gender_ == 2) {
	 		gender = "女";
	 	}else {
	 		gender = "未知";
	 	}
	 	return gender;
	 }</pre>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2009/%e4%bd%bf%e7%94%a8labelfunction%e5%a4%84%e7%90%86datagrid%e6%98%be%e7%a4%ba%e5%86%85%e5%ae%b9/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[小技巧] 通过监听DataGrid的ListEvent.Change控制button是否可用.</title>
		<link>http://liguoliang.com/2008/%e5%b0%8f%e6%8a%80%e5%b7%a7-%e9%80%9a%e8%bf%87%e7%9b%91%e5%90%acdatagrid%e7%9a%84listeventchange%e6%8e%a7%e5%88%b6button%e6%98%af%e5%90%a6%e5%8f%af%e7%94%a8/</link>
		<comments>http://liguoliang.com/2008/%e5%b0%8f%e6%8a%80%e5%b7%a7-%e9%80%9a%e8%bf%87%e7%9b%91%e5%90%acdatagrid%e7%9a%84listeventchange%e6%8e%a7%e5%88%b6button%e6%98%af%e5%90%a6%e5%8f%af%e7%94%a8/#comments</comments>
		<pubDate>Sun, 28 Dec 2008 13:01:24 +0000</pubDate>
		<dc:creator>Guoliang</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[ListEvent]]></category>

		<guid isPermaLink="false">http://liguoliang.com/2008/12/643/</guid>
		<description><![CDATA[如下, 在没有点击[默认]是, 后两个button的enable=false;
<a href="http://liguoliang.com/wp-content/uploads/2008/12/image2.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="180" alt="image" src="http://liguoliang.com/wp-content/uploads/2008/12/image-thumb2.png" width="244" border="0" /></a> 
点击一个之后:
<a href="http://liguoliang.com/wp-content/uploads/2008/12/image3.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="175" alt="image" src="http://liguoliang.com/wp-content/uploads/2008/12/image-thumb3.png" width="244" border="0" /></a> 
简单实现方法:
_dataGridStudent.addEventListener(ListEvent.CHANGE, onSlectedChange);
对应监听函数:

		_actionEd<p class='read-more'><a href='http://liguoliang.com/2008/%e5%b0%8f%e6%8a%80%e5%b7%a7-%e9%80%9a%e8%bf%87%e7%9b%91%e5%90%acdatagrid%e7%9a%84listeventchange%e6%8e%a7%e5%88%b6button%e6%98%af%e5%90%a6%e5%8f%af%e7%94%a8/'>More...</a></p><p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></description>
			<content:encoded><![CDATA[<p>如下, 在没有点击[默认]是, 后两个button的enable=false;</p>
<p><a href="http://liguoliang.com/wp-content/uploads/2008/12/image2.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="180" alt="image" src="http://liguoliang.com/wp-content/uploads/2008/12/image-thumb2.png" width="244" border="0" /></a> </p>
<p>点击一个之后:</p>
<p><a href="http://liguoliang.com/wp-content/uploads/2008/12/image3.png"><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="175" alt="image" src="http://liguoliang.com/wp-content/uploads/2008/12/image-thumb3.png" width="244" border="0" /></a> </p>
<p>简单实现方法:</p>
<pre class="java" name="code">_dataGridStudent.addEventListener(ListEvent.CHANGE, onSlectedChange);</pre>
<p>对应监听函数:</p>
<pre class="java" name="code">
		_actionEdit.enabled = _dataGridStudent.selectedItem != null;
		_actionDelete.enabled = _dataGridStudent.selectedItem != null;
</pre>
<p>这样实现了:<br />
1.无点击时不可用<br />
2.点击且有选择时可用<br />
3. 如点击之后, 按住Ctrl点击, 可取消对其选择, 这时将置按钮于不可用状态.</p>
<p><p>

----------Post from: <a href="http://liguoliang.com">@LiGuoliang.com, 欢迎回来~</a>----------</p></p>
]]></content:encoded>
			<wfw:commentRss>http://liguoliang.com/2008/%e5%b0%8f%e6%8a%80%e5%b7%a7-%e9%80%9a%e8%bf%87%e7%9b%91%e5%90%acdatagrid%e7%9a%84listeventchange%e6%8e%a7%e5%88%b6button%e6%98%af%e5%90%a6%e5%8f%af%e7%94%a8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

