ArrayCollection의 속성값으로 인덱스 구하기(how to get the index by ArrayCollection's property)
Adobe Platform/Flex 2013. 1. 2. 16:08
다음은 ArrayCollection의 속성값으로 인덱스를 구하는 예제입니다. getItemIndex() 메소드는 객체 자체를 참조하기 때문에, 속성값으로 Index를 구할 수 없습니다.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
[Bindable]public var dataList:ArrayCollection = new ArrayCollection([{name:"Kevin"}, {name:"Tom"}]);
public function getItemIndexByProperty(array:ArrayCollection, property:String, value:String):Number
{
for (var i:Number = 0; i < array.length; i++)
{
var obj:Object = Object(array[i])
if (obj[property] == value)
return i;
}
return -1;
}
protected function creationCompleteHandler():void
{
var selectedIndex:int = getItemIndexByProperty(dataList, "name", myList.selectedItem.name);
Alert.show(selectedIndex.toString() + " : " + dataList.getItemAt(selectedIndex).name);
}
]]>
</fx:Script>
<s:List dataProvider="{dataList}" itemRenderer="IR" click="creationCompleteHandler();" id="myList"/>
</s:Application>
*source : http://stackoverflow.com/questions/8585300/flex-arraycollection-getitemindex-always-return-1
*source code :getItemIndexExample.fxp
'Adobe Platform > Flex' 카테고리의 다른 글
Actionscript에서 array 속성 (0) | 2013.01.04 |
---|---|
ArrayList vs ArrayCollection (0) | 2013.01.02 |
Flex에서 문서 보여주기 (0) | 2012.12.18 |
Flex itemRenderers에 대한 이해(Understanding Flex itemRenderers) (0) | 2012.12.04 |
사용자 이벤트 송출하기 (Dispatching Cumstom Event) (0) | 2012.11.20 |