facebook twitter hatena line email

Flash/datagrid/ドラッグ移動

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
<?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"
         creationComplete="initApp()"
         >
  <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
  </fx:Declarations>
  <fx:Script>
    <![CDATA[
    import mx.collections.ArrayCollection;
    import mx.events.DragEvent;
    [Bindable] 
    private var _list:ArrayCollection = new ArrayCollection(
      [
        { "id":"0", "data":"test0" },
        { "id":"1", "data":"test1" },
        { "id":"2", "data":"test2" }
      ]
    );
    public function initApp():void
    {
      dataGrid1.addEventListener(DragEvent.DRAG_COMPLETE, dragComplete);
    }
    private function dragComplete(event:DragEvent):void
    {
      for (var i:String in event.target.dataProvider) {
        trace(event.target.dataProvider[i].id);
      }
    }
    ]]>
  </fx:Script>
  <mx:DataGrid id="dataGrid1" dataProvider="{_list}" rowCount="4" sortableColumns="false" 
          dragMoveEnabled="true" dragEnabled="true" dropEnabled="true"
          >
    <mx:columns>
      <mx:DataGridColumn headerText="id" width="100" dataField="id" />
      <mx:DataGridColumn headerText="データ" width="100" dataField="data" />
    </mx:columns>
  </mx:DataGrid>
</s:Application>

注意

selectedIndexに値を設定する場合は一度validateNow()を実行するしないと値が設定されない

dataGrid1.validateNow();
dataGrid1.selectedIndex = 1;