如何使用flex中的复选框选择datagrid中的所有行或选定行

时间:2013-01-17 10:13:18

标签: flex datagrid checkbox flex4 flex4.5

实际上我需要使用复选框代码选择datagrid中的行。然后,如果我选择一行,那些行只会打印。如果我没有选择行,请将所有行打印出来。 Plz帮助代码。

                    <mx:DataGrid id="dg" dataProvider="{dp}" allowMultipleSelection="true" selectable="true" height="100%" width="100%" >
                        <mx:columns>

                                <mx:DataGridColumn headerText="Select" dataField="Select" textAlign="center">
                                    <mx:itemRenderer>
                                        <fx:Component id="chkGrid">
                                            <mx:CheckBox click="data.Select=!data.Select" selected="{data.Select}"/>
                                        </fx:Component>
                                    </mx:itemRenderer>
                                </mx:DataGridColumn>

                            <mx:DataGridColumn headerText="Name" dataField="Nname"/>
                            <mx:DataGridColumn headerText="Metal Weight" dataField="metalwgt"/>
                            <mx:DataGridColumn headerText="Diamond Weight" dataField="diamondwgt"/>
                            <mx:DataGridColumn headerText="Metal Carat" dataField="carat"/>
                            <mx:DataGridColumn headerText="Price" dataField="price"/>
                            <mx:DataGridColumn headerText="ImagePath" dataField="imagePathTxt" visible="false"/>
                        </mx:columns>
                    </mx:DataGrid>

                </s:VGroup>
            </s:BorderContainer>

        </s:VGroup>
    </s:HGroup>
</s:BorderContainer>

1 个答案:

答案 0 :(得分:0)

假设print_clickHandler是打印按钮的点击处理程序..那么这就是获取这些选定行的代码的样子..

protected function print_clickHandler():void
{
    var PrintCollection:ArrayCollection = new ArrayCollection;
    // The below code will get you the selected rows..
    for each(var item:Object in dp)
    {
        if(item.hasOwnProperty("Select") && item.Select)
          PrintCollection.addItem(item);
    }

    // PrintCollection is the selected rows collection which you need to print
    // or the below code will also get you the selected rows.. Either of it will work       
    dp.filterFunction = function(item:Object):Boolean
    {
        return (item.hasOwnProperty("Select") && item.Select);
    }
    dp.refresh();

    // dp is the selected rows collection which you need to print
}