我有一个数据网格,我试图让用户点击一个按钮来选择我的dataGrid的特定列。我正在尝试选择一个特定的列,以便用户可以将其复制并粘贴到excel中。我可以选择所有列和行显示,但只需要一个特定的coloumn。如果有人有任何建议,那就太棒了 - 谢谢!
protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var indexArr:Array = [];
for(var i:int=0; i<budgetGrid.dataProvider.length; i++) {
indexArr.push(i);
}
budgetGrid.selectedIndices= indexArr;
}
答案 0 :(得分:0)
以下代码完美无缺。当您点击DataGrid
任意一列的标题时,它也会为所有项目设置选择颜色。然后,您可以单击按钮选择列中的所有项目,然后单击另一个按钮进行打印。如果再次单击所选列,它将删除背景选择颜色,并且还将使所选列无效。我正在粘贴您可以在Flex中轻松运行的完整代码。它正在使用Halo Datagrid
。您可以根据需要进行一些清理。
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)"
>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.events.DataGridEvent;
import mx.events.FlexEvent;
var selectedColumnItems:ArrayCollection = new ArrayCollection();
var selectedColumn:DataGridColumn;
var selectedColor:String ="0x7FCEFF";
protected function button1_clickHandler(event:MouseEvent):void
{
if(selectedColumn) {
selectedColumnItems.removeAll();
for each(var obj:Object in dg.dataProvider) {
selectedColumnItems.addItem(obj[selectedColumn.dataField]);
}
}
}
protected function button2_clickHandler(event:MouseEvent):void
{
if(selectedColumn){
for each(var obj:String in selectedColumnItems) {
Alert.show(obj);
}
}
}
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
dg.addEventListener(DataGridEvent.HEADER_RELEASE, selectColumn);
}
private function selectColumn(event:DataGridEvent):void
{
//Set some background color to give the impression that the column is selected.
var existingColor:String = dg.columns[event.columnIndex].getStyle("backgroundColor");
if(existingColor == null ){
selectedColumn = dg.columns[event.columnIndex];
selectedColumn.setStyle("backgroundColor", "0x7FCEFF");
}
else
{
selectedColumn = null;
dg.columns[event.columnIndex].setStyle("backgroundColor", null);
}
event.stopImmediatePropagation();
}
]]>
</mx:Script>
<mx:VBox width="100%">
<mx:DataGrid id="dg">
<mx:dataProvider>
<mx:Array>
<mx:Object age="24" sex="f" name="Susan" joinDate="{new Date(2007, 5, 15)}" />
<mx:Object age="36" sex="f" name="Ashley" joinDate="{new Date(1998, 7, 20)}" />
<mx:Object age="24" sex="f" name="Jennifer" joinDate="{new Date(2001, 3, 24)}" />
<mx:Object age="19" sex="f" name="Emma" joinDate="{new Date(2002, 3, 24)}" />
<mx:Object age="44" sex="f" name="Carol" joinDate="{new Date(1999, 9, 16)}" />
</mx:Array>
</mx:dataProvider>
</mx:DataGrid>
<mx:Button label="Copy Selected Column" click="button1_clickHandler(event)"/>
<mx:Button label="Print Selection" click="button2_clickHandler(event)"/>
</mx:VBox>
</mx:Application>