itemRender---之outerDocument : 使用itemRender 和item Editor时想要访问外部变量或方法的关键字

You use the <mx:Component> tag to define an inline item renderer or item editor in an MXML file.

The <mx:Component> tag defines a new scope within an MXML file, where the local scope of the item renderer or item editor is defined by the MXML code block delimited by the <mx:Component> and </mx:Component> tags. To access elements outside of the local scope of the item renderer or item editor, you prefix the element name with the outerDocument keyword.

For example, you define one variable named localVar in the scope of the main application, and another variable with the same name in the scope of the item renderer. From within the item renderer, you access the application's localVar by prefixing it with outerDocument keyword, as the following example shows:
Flex代码
<?xml version="1.0"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <!--[CDATA[ // Variable in the Application scope. [Bindable] public var localVar:String="Application localVar"; // Data includes URL to album cover. private var initDG:Array = [{ Artist:'Pavement', Album:'Slanted and Enchanted', Price:11.99,Cover:'http://localhost:8100/f15/slanted.jpg'},{ Artist:'Pavement', Album:'Brighten the Corners', Price:11.99,Cover:'http://localhost:8100/f15/brighten.jpg'}]; ]]> </mx:Script> <mx:DataGrid id="myGrid" dataProvider="{initDG}" variableRowHeight="true"> <mx:columns> <mx:DataGridColumn dataField="Artist"/> <mx:DataGridColumn dataField="Album"/> <mx:DataGridColumn dataField="Cover"> <mx:itemRenderer> <mx:Component> <mx:VBox> <mx:Script> <![CDATA[ // Variable in the renderer scope. public var localVar:String="Renderer localVar"; ]]--> </mx:Script> <mx:Text id="albumName" width="100%" selectable="false" text="{data.Album}"/> <mx:Image id="albumImage" height="45" source="{data.Cover}"/> <mx:TextArea text="{'Renderer localVar= ' + localVar}"/> <mx:TextArea text="{'App localVar= ' + outerDocument.localVar}"/> </mx:VBox> </mx:Component> </mx:itemRenderer> </mx:DataGridColumn> <mx:DataGridColumn dataField="Price"/> </mx:columns> </mx:DataGrid> </mx:Application>



One use of the outerDocument keyword is to initialize the data provider of a control within the inline item editor. For example, you can use a web service, or other mechanism, to pass data into the application, such as the list of U.S. states. You can then initialize all ComboBox controls that are used as item editors from a single property of the application that contains the list of U.S. states.

MXML Syntax

The <mx:Component> tag has the following syntax:

<mx:Component id="" className="" >

... child tags

... </mx:Component>

You cannot create an empty <mx:Component></mx:Component> tag; you must define at least one child tag within the <mx:Component></mx:Component> tags.

The id property lets you specify an identifier for the inline component so that you can use it as the source for a data binding expression.

The className property lets you specify the name of the class generated by Flex for the inline component so that you can reference the elements of the component in ActionScript. For more information, see the Using Item Renderers and Item Editors chapter in the Flex Developer's Guide book.



thaks:http://blog.csdn.net/tingqier/archive/2008/10/17/3090326.aspx
Flex代码
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2008/05/09/using-a-slider-control-as-a-datagrid-column-item-renderer-in-flex/ --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"> <mx:ArrayCollection id="arrColl"> <mx:source> <mx:Array> <mx:Object label="Student A" score="85" /> <mx:Object label="Student B" score="48" /> <mx:Object label="Student C" score="71" /> <mx:Object label="Student D" score="88" /> <mx:Object label="Student E" score="24" /> <mx:Object label="Student F" score="64" /> <mx:Object label="Student G" score="76" /> <mx:Object label="Student H" score="76" /> <mx:Object label="Student I" score="93" /> <mx:Object label="Student J" score="88" /> <mx:Object label="Student K" score="48" /> <mx:Object label="Student L" score="76" /> </mx:Array> </mx:source> </mx:ArrayCollection> <mx:DataGrid id="dataGrid" dataProvider="{arrColl}" selectable="false" rowCount="6" width="500"> <mx:columns> <mx:DataGridColumn dataField="label" /> <mx:DataGridColumn dataField="score" /> <mx:DataGridColumn dataField="score"> <mx:itemRenderer> <mx:Component> <mx:HBox horizontalScrollPolicy="off" verticalScrollPolicy="off"> <mx:Script> <!--[CDATA[ import mx.events.SliderEvent; private function slider_change(evt:SliderEvent):void { data.score = evt.value; outerDocument.arrColl.refresh(); } ]]--> </mx:Script> <mx:HSlider minimum="0" maximum="100" value="{data.score}" liveDragging="true" snapInterval="1" width="100%" change="slider_change(event);" /> </mx:HBox> </mx:Component> </mx:itemRenderer> </mx:DataGridColumn> </mx:columns> </mx:DataGrid> </mx:Application>  

from:http://blog.minidx.com/2008/05/10/837.html

你可能感兴趣的:(itemRender---之outerDocument : 使用itemRender 和item Editor时想要访问外部变量或方法的关键字)