如何在flex中从DataGrid获取选定值(使用checkBox)

时间:2012-01-02 18:12:19

标签: flex actionscript datagrid checkbox

我有一个datagrid,它从XML文件中获取值(使用PHP和HTTP中的HTTP请求从数据库获取此xml文件)。我在数据网格的每一行都创建了一个复选框。这是我的要求: 我想选择两个或三个复选框,并希望以某种形式获得特定ROW的所有值,首选arraycollection(这样我可以将此数组直接传递到条形图)..可以有人帮助我我是新手。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="siteData.send()">
<mx:Script>
    <![CDATA[
        import mx.collections.XMLListCollection;
        import mx.controls.*;
        import mx.events.ListEvent;
        import mx.rpc.events.ResultEvent;
        import mx.controls.Alert;
        [Bindable] private var fullXML:XMLList; 
        private function contentHandler(evt:ResultEvent):void{
            fullXML = evt.result.values;
        }

    ]]>
</mx:Script>
<mx:VBox>
    <mx:Label text="This Data Grid is loading the full XML file"/>
    <mx:DataGrid width="600"  id="datagrid" dataProvider="{fullXML}">

        <mx:columns>
            <mx:DataGridColumn headerText="Select">
                <mx:itemRenderer>
                    <mx:Component>
                        <mx:HBox horizontalAlign="center">

                            <mx:CheckBox id="check"/>
                        </mx:HBox>
                    </mx:Component>
                </mx:itemRenderer>
            </mx:DataGridColumn>
            <mx:DataGridColumn dataField="release_version" headerText="Release"/>
            <mx:DataGridColumn dataField="build" headerText="build"/>
            <mx:DataGridColumn dataField="time_login" headerText="time_login"/>
            <mx:DataGridColumn dataField="time_tunnel" headerText="time_tunnel"/>
            <mx:DataGridColumn dataField="rate_login" headerText="time_tunnel"/>
            <mx:DataGridColumn dataField="rate_tunnel" headerText="rate_tunnel"/>
        </mx:columns>
    </mx:DataGrid>
</mx:VBox>
<mx:HTTPService url="http://localhost/php_genxml.php" id="siteData" result="contentHandler(event)" resultFormat="e4x"/>
</mx:Applicaton>

我想选择一些复选框,并希望获取数据网格中所有字段的值对应于该复选框,有人可以帮助我获取所选值(选中复选框的值)在flex和动作脚本中。

1 个答案:

答案 0 :(得分:1)

<mx:itemRenderer>
                    <mx:Component>
                        <mx:HBox horizontalAlign="center" verticalAlign="middle">
                            <mx:Script>
                                <![CDATA[
                                    var objTemp:Object = new Object();

                                    override public function set data(value:Object):void
                                    {
                                        if(value != null)
                                        {
                                            var xml:XML = XML(value);
                                            super.data = value;
                                            objTemp = outerDocument.xmlToObject(xml.toString());
                                            if(objTemp.story['quiz_score'] != null)
                                            {
                                                chkAssignment.visible = false;
                                            }
                                            else
                                            {
                                                chkAssignment.visible = true;
                                            }
                                            if(objTemp.story.is_selected == false)
                                            {
                                                chkAssignment.selected = false;
                                            }
                                            else
                                            {
                                                chkAssignment.selected = true;
                                            }

                                        }
                                    }

                                    private function deleteAssignment():void
                                    {


                                        if(chkAssignment.selected)
                                        {
                                            outerDocument.isChanged = true;

                                            objTemp.story.is_selected = true;
                                            var xml:XML = outerDocument.objectToXML(objTemp,"record");

                                            var xmlList:XMLList = xml.children();
                                            xml = xmlList[0] as XML;

                                            outerDocument.dgListeningLog.dataProvider[outerDocument.dgListeningLog.selectedIndex] = xml;

                                            outerDocument.arrAssignment.push({"story_name": XML(outerDocument.dgListeningLog.selectedItem).story_title.toString() ,"student_assignmentId": XML(outerDocument.dgListeningLog.selectedItem).assignment_id.toString(),"session_key": XML(outerDocument.dgListeningLog.selectedItem).session_key.toString(),"selectedIndex": outerDocument.dgListeningLog.selectedIndex.toString()});
                                        }
                                        else
                                        {
                                            outerDocument.isChanged = true;

                                            objTemp.story.is_selected = false;
                                            var xml:XML = outerDocument.objectToXML(objTemp,"record");

                                            var xmlList:XMLList = xml.children();
                                            xml = xmlList[0] as XML;

                                            outerDocument.dgListeningLog.dataProvider[outerDocument.dgListeningLog.selectedIndex] = xml;

                                            for(var i:int =0; i < outerDocument.arrAssignment.length; i++)
                                            {
                                                if(outerDocument.arrAssignment[i].selectedIndex == outerDocument.dgListeningLog.selectedIndex)
                                                {
                                                    outerDocument.arrAssignment.splice(i,1);
                                                    break;
                                                }
                                            }
                                        } 

                                    }

                                ]]>
                            </mx:Script>
                            <mx:CheckBox id="chkAssignment" change="{deleteAssignment();}"/>
                        </mx:HBox>
                    </mx:Component>
                </mx:itemRenderer>

这里我将选定的值或数组存储在另一个数组中,当单击“删除”按钮时,它将检查并删除作为dataGrid的数据提供者的主数组中的值。

如果您在滚动数据网格时遇到问题CheckBox显示的错误值比从代码中复制以下方法错误:

覆盖公共函数集数据(value:Object):void

主要使用两个函数......

public function objectToXML(obj:Object, qname:String):XML 
        {
            var qName:QName = new QName(qname);
            var xmlDocument:XMLDocument = new XMLDocument();
            var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
            var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
            var xml:XML = new XML(xmlDocument.toString());
            return xml;
        }

        public function xmlToObject(value:String):Object 
        {
            var xmlStr:String = value.toString();
            var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
            var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
            var resultObj:Object = decoder.decodeXML(xmlDoc);
            return resultObj;
        }