2009-05-06 10 views
0

我有一個帶有2個Horizo​​ntalList控件的Flex3項目;其中一個拖動已啓用&拖放。兩個控件始終具有相同數量的項目,並且相關...第一個控件的索引0與第二個控件的索引0匹配,以此類推。Flex拖放一個Horizo​​ntalList控件的重新排序 - 查找FROM和TO索引?

當然,使用拖放操作重新排序第二個控件時,我希望第一個控件中的項目反映相同的重新排序。我想我可以處理控件的實際更新,但我無法找到已通過拖動&拖放移動的項目的索引。

使用的控制dragDrop方法,它允許拖放,並在調試細讀的event.currentTarget(而不是event.targetbecause the docs say so)中的內容顯示了一個名爲selectedIndex這似乎持有指數的公共屬性,但我沒有看到任何屬性會告訴我索引。

我可以俯瞰酒店嗎?我必須根據別的東西推斷它嗎?有一個更好的方法嗎?

更新:可接受的解決方案也可能是迭代Horizo​​ntalList將內容並保存內部索引值然後可用於爲其他列表重新排序的數據提供程序的陣列;但我無法弄清楚如何迭代列表內容,所以我就像被卡住了一樣。有什麼幫助嗎?

這裏是我的代碼:

main.mxml:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:com="components.*" 
    layout="absolute" 
> 
    <mx:Script> 
     <![CDATA[ 
      import mx.events.DragEvent; 
      import mx.controls.Alert; 
      import mx.collections.ArrayCollection; 

      //these two variables are junk data to represent 
      //what we will get back from our data services... 
      [Bindable] 
      private var btnData:ArrayCollection = new ArrayCollection(
       [ 
        {title:'title1', index:0}, 
        {title:'title2', index:1}, 
        {title:'title3', index:2}, 
        {title:'title4', index:3} 
       ] 
      ); 
      [Bindable] 
      private var chainData:ArrayCollection = new ArrayCollection(
       [ 
        {tmp:'testing 1'}, 
        {tmp:'testing 2'}, 
        {tmp:'testing 3'}, 
        {tmp:'testing 4'} 
       ] 
      ); 

      //handle button re-ordering 
      private function handleBtnReorder(event:DragEvent):void{ 
       Alert.show(event.action.toString()); 
      } 
     ]]> 
    </mx:Script> 

    <mx:HorizontalList 
     id="ChainView" 
     dataProvider="{chainData}" 
     itemRenderer="components.ItemRenderers.ChainLinkRenderer" 
     left="20" 
     right="20" 
     top="20" 
     height="300" 
     rowHeight="300" 
     columnWidth="500" 
     rollOverColor="#ffffff" 
     selectionColor="#eeeeee" 
    /> 

    <mx:HorizontalList 
     id="btnList" 
     dataProvider="{btnData}" 
     dragEnabled="true" 
     dropEnabled="true" 
     dragMoveEnabled="true" 
     dragDrop="handleBtnReorder(event)" 
     itemRenderer="components.ItemRenderers.BtnListRenderer" 
     horizontalCenter="true" 
     left="20" 
     right="20" 
     top="320" 
     height="50" 
     rowHeight="35" 
     rollOverColor="#ffffff" 
     selectionColor="#ffffff" 
    /> 

</mx:Application> 

Alert.show(...)是不是有顯示有用的東西,我已經設置在該行斷點,這樣我可以檢查事件參數。

我不認爲自定義itemRenderer是重要的(但它們沒有太多代碼),所以爲了簡潔起見,我將它們排除在外。如果你認爲他們重要,請告訴我,我會在這裏編輯它們。

回答

2

事實證明,解決方案是從使用dragDrop事件切換到dragComplete事件;此時綁定的dataProvider已更新以反映重新排序。

-1

這就是模型定位器模式的原因!

您必須將您的dataProvider移動到單個類(例如Model),而不是您的項呈示器可以輕鬆訪問它。沒有意義,您的項目具有屬性Index,因爲它已經由[ArrayCollection] .getItemIndex(..)