Repeater 相关的几个重要属性和方法
Repeater类的几个重要属性和方法:
currentItem,在repeater循环过程中,这应该是最为重要的了,这个属性代表了当前正在处理的dataProvider对应的对象,一般用于与组件的属性(例如:text、label、hint等)绑定时使用
getRepeaterItem(index: integer = null),repeater一旦循环完毕,currentItem就不能够再使用,可以使用getRepeaterItem()获取当前组件对应的数据对象;在多个repeater嵌套的情况下,可以指定index参数,在为空的情况,表示是最内层的repeater,如果等于0,表示是最外层repeater,1为第二层,依此类推
instanceIndices,用于在事件中获取当前组件对应的数据索引编号,也就是dataProvider中的索引,从而可以通过这个编号访问底层数据
repeaters属性,是获取组件对应的repeater对象,目前我还没有发现这个属性有什么用处
repeaterIndices,当repeater从0开始循环(也就是说startIndex="0")的时候,和instanceIndices是一样的;但是,当startIndex不等0的时候,repeaterIndices始终是数据对象在dataProvider中的真正索引,而不像instanceIndices始终是从0开始编号的
- currentItem
- getRepeaterItem()
- instanceIndices
- repeaters
- repeaterIndices
currentItem,在repeater循环过程中,这应该是最为重要的了,这个属性代表了当前正在处理的dataProvider对应的对象,一般用于与组件的属性(例如:text、label、hint等)绑定时使用
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
public function labelTrace():void {
for (var i:int = 0; i < nameLabel.length; i++)
trace(nameLabel[i].text);
}
]]>
</mx:Script>
<mx:Model id="catalog" source="../assets/repeater/catalog.xml"/>
<mx:Label id="title" text="Products:"/>
<mx:Repeater id="r" dataProvider="{catalog.product}"
startingIndex="1">
<mx:VBox id="vb">
<mx:Label id="nameLabel"
text="{r.currentItem.name}: ${r.currentItem.price}"
width="200"/>
<mx:Label id="shipLabel"
text="Free shipping: {r.currentItem.freeship}"/>
<mx:Spacer/>
</mx:VBox>
</mx:Repeater>
<mx:Button label="Trace" click="labelTrace();"/>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
public function labelTrace():void {
for (var i:int = 0; i < nameLabel.length; i++)
trace(nameLabel[i].text);
}
]]>
</mx:Script>
<mx:Model id="catalog" source="../assets/repeater/catalog.xml"/>
<mx:Label id="title" text="Products:"/>
<mx:Repeater id="r" dataProvider="{catalog.product}"
startingIndex="1">
<mx:VBox id="vb">
<mx:Label id="nameLabel"
text="{r.currentItem.name}: ${r.currentItem.price}"
width="200"/>
<mx:Label id="shipLabel"
text="Free shipping: {r.currentItem.freeship}"/>
<mx:Spacer/>
</mx:VBox>
</mx:Repeater>
<mx:Button label="Trace" click="labelTrace();"/>
</mx:Application>
getRepeaterItem(index: integer = null),repeater一旦循环完毕,currentItem就不能够再使用,可以使用getRepeaterItem()获取当前组件对应的数据对象;在多个repeater嵌套的情况下,可以指定index参数,在为空的情况,表示是最内层的repeater,如果等于0,表示是最外层repeater,1为第二层,依此类推
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
public function clicker(cName:String):void {
foolabel.text=cName;
}
]]>
</mx:Script>
<mx:Label id="foolabel" text="foo"></mx:Label>
<mx:Model id="data">
<color>
<colorName>Red</colorName>
<colorName>Yellow</colorName>
<colorName>Blue</colorName>
</color>
</mx:Model>
<mx:ArrayCollection id="myAC" source="{data.colorName}"/>
<mx:Repeater id="myrep" dataProvider="{myAC}">
<mx:Button click="clicker(event.currentTarget.getRepeaterItem());"
label="{myrep.currentItem}"/>
</mx:Repeater>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
public function clicker(cName:String):void {
foolabel.text=cName;
}
]]>
</mx:Script>
<mx:Label id="foolabel" text="foo"></mx:Label>
<mx:Model id="data">
<color>
<colorName>Red</colorName>
<colorName>Yellow</colorName>
<colorName>Blue</colorName>
</color>
</mx:Model>
<mx:ArrayCollection id="myAC" source="{data.colorName}"/>
<mx:Repeater id="myrep" dataProvider="{myAC}">
<mx:Button click="clicker(event.currentTarget.getRepeaterItem());"
label="{myrep.currentItem}"/>
</mx:Repeater>
</mx:Application>
instanceIndices,用于在事件中获取当前组件对应的数据索引编号,也就是dataProvider中的索引,从而可以通过这个编号访问底层数据
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]
public var myArray:Array=[1,2,3,4,5,6,7,8];
]]>
</mx:Script>
<mx:ArrayCollection id="myAC" source="{myArray}"/>
<mx:Repeater id="list" dataProvider="{myAC}" count="4" startingIndex="2">
<mx:HBox>
<mx:Button label="Click Me"
click="myText[event.target.instanceIndices].text=
event.target.instanceIndices.toString();"/>
<mx:TextInput id="myText"/>
</mx:HBox>
</mx:Repeater>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]
public var myArray:Array=[1,2,3,4,5,6,7,8];
]]>
</mx:Script>
<mx:ArrayCollection id="myAC" source="{myArray}"/>
<mx:Repeater id="list" dataProvider="{myAC}" count="4" startingIndex="2">
<mx:HBox>
<mx:Button label="Click Me"
click="myText[event.target.instanceIndices].text=
event.target.instanceIndices.toString();"/>
<mx:TextInput id="myText"/>
</mx:HBox>
</mx:Repeater>
</mx:Application>
repeaters属性,是获取组件对应的repeater对象,目前我还没有发现这个属性有什么用处
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
[Bindable]
public var myArray:Array=[1,2];
]]>
</mx:Script>
<mx:ArrayCollection id="myAC" source="{myArray}"/>
<mx:Repeater id="repeater1" dataProvider="{myAC}">
<mx:Repeater id="repeater2" dataProvider="{myAC}">
<mx:Button
label="[{repeater1.currentIndex},{repeater2.currentIndex}]"
click="Alert.show(event.target.repeaters[1].id);"/>
</mx:Repeater>
</mx:Repeater>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
[Bindable]
public var myArray:Array=[1,2];
]]>
</mx:Script>
<mx:ArrayCollection id="myAC" source="{myArray}"/>
<mx:Repeater id="repeater1" dataProvider="{myAC}">
<mx:Repeater id="repeater2" dataProvider="{myAC}">
<mx:Button
label="[{repeater1.currentIndex},{repeater2.currentIndex}]"
click="Alert.show(event.target.repeaters[1].id);"/>
</mx:Repeater>
</mx:Repeater>
</mx:Application>
repeaterIndices,当repeater从0开始循环(也就是说startIndex="0")的时候,和instanceIndices是一样的;但是,当startIndex不等0的时候,repeaterIndices始终是数据对象在dataProvider中的真正索引,而不像instanceIndices始终是从0开始编号的
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]
public var myArray:Array = [1,2,3,4,5,6,7,8];
]]>
</mx:Script>
<mx:Repeater id="list" dataProvider="{myArray}"
startingIndex="2" count="4">
<mx:HBox>
<mx:Button id="b" label="Click Me"
click="myText[event.target.repeaterIndices-list.startingIndex].text=
event.target.repeaterIndices.toString();"/>
<mx:TextInput id="myText"/>
</mx:HBox>
</mx:Repeater>
</mx:Application>
从上面的例子看,repeaterIndices应该是从2开始的,而instanceIndices是从0开始编号的。<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
[Bindable]
public var myArray:Array = [1,2,3,4,5,6,7,8];
]]>
</mx:Script>
<mx:Repeater id="list" dataProvider="{myArray}"
startingIndex="2" count="4">
<mx:HBox>
<mx:Button id="b" label="Click Me"
click="myText[event.target.repeaterIndices-list.startingIndex].text=
event.target.repeaterIndices.toString();"/>
<mx:TextInput id="myText"/>
</mx:HBox>
</mx:Repeater>
</mx:Application>