溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在Flex數(shù)據(jù)綁定中使用數(shù)組

發(fā)布時(shí)間:2021-11-03 17:55:30 來(lái)源:億速云 閱讀:143 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)如何在Flex數(shù)據(jù)綁定中使用數(shù)組的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

在Flex數(shù)據(jù)綁定中使用數(shù)組

在使用數(shù)組進(jìn)行工作時(shí),比如Array或者ArrayCollection對(duì)象,可以把數(shù)組作為Flex數(shù)據(jù)綁定表達(dá)式的源或目的。
注意:當(dāng)使用數(shù)組作為綁定源時(shí),應(yīng)該使用ArrayCollection類型的數(shù)組,因?yàn)锳rrayCollection類在數(shù)組或數(shù)組元素發(fā)生變化時(shí)能夠發(fā)出事件來(lái)觸發(fā)Flex數(shù)據(jù)綁定。比如,對(duì)ArrayCollection.addItem(),ArrayCollection.addItemAt(),ArrayCollection.removeItem(),以及ArrayCollection.removeItemAt()方法的調(diào)用都會(huì)觸發(fā)Flex數(shù)據(jù)綁定。

綁定到數(shù)組

通常將數(shù)組綁定給Flex控件的dataProvider屬性,下面范例說(shuō)明將數(shù)組綁定用于List控件:

<?xmlversionxmlversion="1.0"?>  <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml">  <![CDATA[  importmx.collections.ArrayCollection;  [Bindable]  publicvarmyAC:ArrayCollection=newArrayCollection([  "One","Two","Three","Four"]);  [Bindable]  publicvarmyAC2:ArrayCollection=newArrayCollection([  "Uno","Dos","Tres","Quatro"]);  ]]>  <mx:Button label="ChangeElement" click="myAC[0]='modOne'"/>  <mx:Button label="AddElement" click="myAC.addItem('newelement');"/>  <mx:Button label="RemoveElement0" click="myAC.removeItemAt(0);"/>  <mx:Button label="ChangeArrayCollection" click="myAC=myAC2"/>

這個(gè)例子定義了一個(gè)ArrayCollection對(duì)象,然后將List控件的dataProvider屬性設(shè)置為對(duì)這個(gè)ArrayCollection的Flex數(shù)據(jù)綁定。當(dāng)修改ArrayCollection對(duì)象中的元素,或者修改對(duì)ArrayCollection對(duì)象的引用,都會(huì)觸發(fā)Flex數(shù)據(jù)綁定。

綁定到數(shù)組中的元素

可以使用數(shù)組中的單個(gè)元素作為Flex數(shù)據(jù)綁定源,如下例所示:

<?xmlversionxmlversion="1.0"?>  <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml">  <![CDATA[  importmx.collections.ArrayCollection;  [Bindable]  publicvarmyAC:ArrayCollection=newArrayCollection([  "One","Two","Three","Four"]);  [Bindable]  publicvarmyAC2:ArrayCollection=newArrayCollection([  "Uno","Dos","Tres","Quatro"]);  ]]>  <mx:Buttonidmx:Buttonid="button1" label="ChangeElement" click="myAC[0]='newOne'"/> <mx:Buttonidmx:Buttonid="button2" label="ChangeArrayCollection" click="myAC=myAC2"/>

如果通過(guò)方括號(hào)語(yǔ)法[]來(lái)指定數(shù)組元素作為Flex數(shù)據(jù)綁定表達(dá)式的源,那么Flex數(shù)據(jù)綁定只在應(yīng)用啟動(dòng)時(shí)觸發(fā),或者在數(shù)組或其引用被更新時(shí)觸發(fā)。當(dāng)這個(gè)數(shù)組元素被更新的時(shí)候不會(huì)觸發(fā)Flex數(shù)據(jù)綁定。
但Flex數(shù)據(jù)綁定表達(dá)式中的myAC.getItemAt(0)則會(huì)在該數(shù)組元素變化時(shí)被觸發(fā)更新。因此,id為text2的Text控件在點(diǎn)擊button1時(shí)會(huì)被更新,而id為text1的Text控件則不會(huì)被更新。
當(dāng)使用數(shù)組中的元素作為Flex數(shù)據(jù)綁定表示的源時(shí),應(yīng)當(dāng)在綁定表達(dá)式中使用ArrayCollection.getItemAt()方法。

點(diǎn)擊button2時(shí)將myAC2拷貝給myAC,這會(huì)觸發(fā)對(duì)數(shù)組元素的所有Flex數(shù)據(jù)綁定而不論它們是如何實(shí)現(xiàn)的。

感謝各位的閱讀!關(guān)于“如何在Flex數(shù)據(jù)綁定中使用數(shù)組”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI