リストコントロールは、すべて allowMultipleSelection プロパティをサポートしています。allowMultipleSelection プロパティを true に設定すると、コントロールの複数のアイテムを同時に選択することができます。例えば、DataGrid コントロールで複数の行を選択して、別の DataGrid コントロールにドラッグ & ドロップできます。
AdvancedDataGrid コントロールには、複数のセルを選択する機能が追加されています。選択したセルは、別の AdvancedDataGrid コントロールにドラッグしたり、クリップボードにコピーしたりできます。また、セルのデータに対するその他のアクションを実行することも可能です。
複数の選択を実行できるように allowMultipleSelection プロパティを設定して、AdvancedDataGrid コントロールの selectionMode プロパティを使用します。selectionMode プロパティのデフォルト値は singleRow で、1 行ずつしか選択できないことを示します。selectionMode プロパティの値は、singleCell、multipleRows、または multipleCells に設定することもできます。
AdvancedDataGrid コントロールでセルまたは行を選択すると、選択した内容に基づいて selectedCells プロパティが更新されます。selectedCells プロパティはオブジェクトの配列で、各オブジェクトには、コントロールで選択されている行またはセルの位置を表す rowIndex プロパティおよび columnIndex プロパティが格納されています。
rowIndex プロパティおよび columnIndex プロパティのデータは、次の表に示すように、selectionMode プロパティの値によって決定されます。
|
selectionMode プロパティの値 |
rowIndex プロパティおよび columnIndex プロパティの値 |
|---|---|
| none |
コントロールでの選択は許可されず、selectedCells は null になります。 |
| singleRow |
行の任意のセルをクリックして、その行を選択します。 選択すると、selectedCells には単一のオブジェクトが格納されます。 [{rowIndex:selectedRowIndex, columnIndex: -1}] |
| multipleRows |
行の任意のセルをクリックして、その行を選択します。
選択すると、selectedCells には選択された各行について 1 つのオブジェクトが格納されます。 [ {rowIndex: selectedRowIndex1, columnIndex: -1}, {rowIndex: selectedRowIndex2, columnIndex: -1}, ... {rowIndex: selectedRowIndexN, columnIndex: -1} ] |
| singleCell |
任意のセルをクリックして、そのセルを選択します。 選択すると、selectedCells には単一のオブジェクトが格納されます。 [{rowIndex: selectedRowIndex, columnIndex:selectedColIndex}] |
| multipleCells |
任意のセルをクリックして、そのセルを選択します。
選択すると、selectedCells には選択された各セルについて 1 つのオブジェクトが格納されます。 [ {rowIndex: selectedRowIndex1, columnIndex: selectedColIndex1}, {rowIndex: selectedRowIndex2, columnIndex: selectedColIndex2}, ... {rowIndex: selectedRowIndexN, columnIndex: selectedColIndexN} ] |
次の例では selectionMode プロパティを multipleCells に設定して、コントロールで複数のセルを選択できるようにしています。このアプリケーションでは keyUp イベントに対するイベントハンドラを使用して、Ctrl+C キーの組み合わせを認識しています。この操作が検出された場合、選択されているセルを AdvancedDataGrid コントロールからシステムのクリップボードにコピーします。
セルをコピーしたら、Flex アプリケーションの別の場所にペーストしたり、Microsoft Excel などの別のアプリケーションにペーストしたりできます。この例では、アプリケーションの下部にある TextArea コントロールにペーストします。
<?xml version="1.0"?>
<!-- dpcontrols/adg/CellSelectADG.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import flash.events.KeyboardEvent;
import flash.system.System;
include "StyleData.as"
// Event handler to recognize when Ctrl-C is pressed,
// and copy the selected cells to the system clipboard.
private function myKeyUpHandler(event:KeyboardEvent):void
{
var keycode_c:uint = 67;
if (event.ctrlKey && event.keyCode == keycode_c)
{
// Separator used between Strings sent to clipboard
// to separate selected cells.
var separator:String = ",";
// The String sent to the clipboard
var dataString:String = "";
// Loop over the selectedCells property.
// Data in selectedCells is ordered so that
// the last selected cell is at the head of the list.
// Process the data in reverse so
// that it appears in the correct order in the clipboard.
var n:int = event.currentTarget.selectedCells.length;
for (var i:int = 0; i < n; i++)
{
var cell:Object = event.currentTarget.selectedCells[i];
// Get the row for the selected cell.
var data:Object =
event.currentTarget.dataProvider[cell.rowIndex];
// Get the name of the field for the selected cell.
var dataField:String =
event.currentTarget.columns[cell.columnIndex].dataField;
// Get the cell data using the field name.
dataString = data[dataField] + separator + dataString;
}
// Remove trailing separator.
dataString =
dataString.substr(0, dataString.length - separator.length);
// Write dataString to the clipboard.
System.setClipboard(dataString);
}
}
]]>
</mx:Script>
<mx:AdvancedDataGrid width="100%" height="100%"
dataProvider="{dpADG}"
selectionMode="multipleCells"
allowMultipleSelection="true"
keyUp="myKeyUpHandler(event);">
<mx:columns>
<mx:AdvancedDataGridColumn dataField="Artist"/>
<mx:AdvancedDataGridColumn dataField="Album"/>
<mx:AdvancedDataGridColumn dataField="Price"/>
</mx:columns>
</mx:AdvancedDataGrid>
<mx:TextArea id="myTA"/>
</mx:Application>
前の例で実行する SWF ファイルは以下のとおりです。
このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート