Apache flex 在Flex中从DataGrid删除选定行

Apache flex 在Flex中从DataGrid删除选定行,apache-flex,actionscript-3,datagrid,Apache Flex,Actionscript 3,Datagrid,我使用ItemRenderer在DataGrid中添加了一个复选框。我已经在下面粘贴了我正在使用的代码 我在DataGrid之外还有一个按钮,当点击这个按钮时,我想删除所有有复选框的行。有人能告诉我怎么做吗?嗨 首先,您的代码无法正常工作,因为并非每个DataGrid行都有自己的ItemRenderer实例。对于具有n个可见行的DataGrid,正好有n个项目呈现器实例。如果您创建的DataGrid不能容纳所有数据,然后选择一些行并向上/向下滚动DataGrid,您可以很容易地检查这一点。你会看

我使用ItemRenderer在DataGrid中添加了一个复选框。我已经在下面粘贴了我正在使用的代码

我在DataGrid之外还有一个按钮,当点击这个按钮时,我想删除所有有复选框的行。有人能告诉我怎么做吗?

首先,您的代码无法正常工作,因为并非每个DataGrid行都有自己的ItemRenderer实例。对于具有n个可见行的DataGrid,正好有n个项目呈现器实例。如果您创建的DataGrid不能容纳所有数据,然后选择一些行并向上/向下滚动DataGrid,您可以很容易地检查这一点。你会看到意想不到的结果

其中一种解决方案可能是选择其他字段,或者在resultArray项属性中选择任何您想要命名的字段。然后可以通过ItemRenderer的数据对象访问此属性。检查以下代码:

<?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" minWidth="955" minHeight="600">


    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            [Bindable]
            private var resultArray:ArrayCollection = new ArrayCollection
                                                ([
                                                {firstName:"1-1",lastName:"1-2",city:"1-3",empCode:"1-4"},
                                                {firstName:"2-1",lastName:"2-2",city:"2-3",empCode:"2-4"},
                                                {firstName:"3-1",lastName:"3-2",city:"3-3",empCode:"3-4"},
                                                {firstName:"4-1",lastName:"4-2",city:"4-3",empCode:"4-4"},
                                                {firstName:"5-1",lastName:"5-2",city:"5-3",empCode:"5-4"},
                                                {firstName:"6-1",lastName:"6-2",city:"6-3",empCode:"6-4"},
                                                {firstName:"7-1",lastName:"7-2",city:"7-3",empCode:"7-4"},
                                                {firstName:"8-1",lastName:"8-2",city:"8-3",empCode:"8-4"},
                                                {firstName:"9-1",lastName:"9-2",city:"9-3",empCode:"9-4"},
                                                {firstName:"10-1",lastName:"10-2",city:"10-3",empCode:"10-4"},
                                            ]);

            protected function button1_clickHandler(event:MouseEvent):void
            {
                for (var i:int=0; i< resultArray.length; i++)
                {
                    if (resultArray[i].selected == true)
                    {
                        resultArray.removeItemAt(i);
                    }
                }

                dgEmployeeInfo.invalidateList();
            }

        ]]>
    </fx:Script>



    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:VGroup>
        <mx:DataGrid id="dgEmployeeInfo" dataProvider="{resultArray}" x="131" y="95" editable="false">          
            <mx:columns>
                <mx:DataGridColumn headerText="Select" rendererIsEditor="true" editorDataField="selected">
                    <mx:itemRenderer>
                        <fx:Component>
                            <mx:HBox>
                                <s:CheckBox id="testChk" click="testChk_clickHandler(event)" selected="{data.selected}">
                                    <fx:Script>
                                        <![CDATA[
                                            [Bindable]
                                            public var cbSelected:Boolean; 
                                            protected function testChk_clickHandler(event:MouseEvent):void
                                            {
                                                data.selected = testChk.selected;  
                                            }
                                        ]]>
                                    </fx:Script>                                
                                </s:CheckBox>
                            </mx:HBox>
                        </fx:Component>
                    </mx:itemRenderer>
                </mx:DataGridColumn>
                <mx:DataGridColumn headerText="First Name" dataField="firstName"/>
                <mx:DataGridColumn headerText="Last Name" dataField="lastName"/>
                <mx:DataGridColumn headerText="City" dataField="city"/>
                <mx:DataGridColumn headerText="Employee Code" dataField="empCode"/>             
            </mx:columns>       
        </mx:DataGrid>
        <mx:Button label="Delete Items" click="button1_clickHandler(event)"/>
    </s:VGroup> 


</s:Application>
我使用ArrayCollection作为数据提供程序,因为向集合对象添加/删除项要比使用数组容易得多

问候。

你好

首先,您的代码无法正常工作,因为并非每个DataGrid行都有自己的ItemRenderer实例。对于具有n个可见行的DataGrid,正好有n个项目呈现器实例。如果您创建的DataGrid不能容纳所有数据,然后选择一些行并向上/向下滚动DataGrid,您可以很容易地检查这一点。你会看到意想不到的结果

其中一种解决方案可能是选择其他字段,或者在resultArray项属性中选择任何您想要命名的字段。然后可以通过ItemRenderer的数据对象访问此属性。检查以下代码:

<?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" minWidth="955" minHeight="600">


    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            [Bindable]
            private var resultArray:ArrayCollection = new ArrayCollection
                                                ([
                                                {firstName:"1-1",lastName:"1-2",city:"1-3",empCode:"1-4"},
                                                {firstName:"2-1",lastName:"2-2",city:"2-3",empCode:"2-4"},
                                                {firstName:"3-1",lastName:"3-2",city:"3-3",empCode:"3-4"},
                                                {firstName:"4-1",lastName:"4-2",city:"4-3",empCode:"4-4"},
                                                {firstName:"5-1",lastName:"5-2",city:"5-3",empCode:"5-4"},
                                                {firstName:"6-1",lastName:"6-2",city:"6-3",empCode:"6-4"},
                                                {firstName:"7-1",lastName:"7-2",city:"7-3",empCode:"7-4"},
                                                {firstName:"8-1",lastName:"8-2",city:"8-3",empCode:"8-4"},
                                                {firstName:"9-1",lastName:"9-2",city:"9-3",empCode:"9-4"},
                                                {firstName:"10-1",lastName:"10-2",city:"10-3",empCode:"10-4"},
                                            ]);

            protected function button1_clickHandler(event:MouseEvent):void
            {
                for (var i:int=0; i< resultArray.length; i++)
                {
                    if (resultArray[i].selected == true)
                    {
                        resultArray.removeItemAt(i);
                    }
                }

                dgEmployeeInfo.invalidateList();
            }

        ]]>
    </fx:Script>



    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:VGroup>
        <mx:DataGrid id="dgEmployeeInfo" dataProvider="{resultArray}" x="131" y="95" editable="false">          
            <mx:columns>
                <mx:DataGridColumn headerText="Select" rendererIsEditor="true" editorDataField="selected">
                    <mx:itemRenderer>
                        <fx:Component>
                            <mx:HBox>
                                <s:CheckBox id="testChk" click="testChk_clickHandler(event)" selected="{data.selected}">
                                    <fx:Script>
                                        <![CDATA[
                                            [Bindable]
                                            public var cbSelected:Boolean; 
                                            protected function testChk_clickHandler(event:MouseEvent):void
                                            {
                                                data.selected = testChk.selected;  
                                            }
                                        ]]>
                                    </fx:Script>                                
                                </s:CheckBox>
                            </mx:HBox>
                        </fx:Component>
                    </mx:itemRenderer>
                </mx:DataGridColumn>
                <mx:DataGridColumn headerText="First Name" dataField="firstName"/>
                <mx:DataGridColumn headerText="Last Name" dataField="lastName"/>
                <mx:DataGridColumn headerText="City" dataField="city"/>
                <mx:DataGridColumn headerText="Employee Code" dataField="empCode"/>             
            </mx:columns>       
        </mx:DataGrid>
        <mx:Button label="Delete Items" click="button1_clickHandler(event)"/>
    </s:VGroup> 


</s:Application>
我使用ArrayCollection作为数据提供程序,因为向集合对象添加/删除项要比使用数组容易得多


关于。

谢谢,但如果我正在从数据库中提取数据,我是否应该为复选框值向数据库添加一个新字段…?是否还需要删除数据库记录?@user594979。然后重点讨论如何用数据库记录识别DataGrid行。您完全不必将所选字段添加到数据库中。继续我的示例,当检测到resultArray[i].selected==true时,您可以向button1\u clickHandler添加一些代码。在代码中,执行SQL DELETE命令构造语句,如从某些表t中删除,其中t.firstNameField=+resultArray[i].firstName+和t.lastNameField=+resultArray[i].lastName。最好的做法可能是将某个recordId字段提取到resultArray。里德斯。谢谢,但如果我从数据库中提取数据,我是否应该为复选框值向数据库中添加一个新字段..?是否还需要删除数据库记录?@user594979。然后重点讨论如何用数据库记录识别DataGrid行。您完全不必将所选字段添加到数据库中。继续我的示例,当检测到resultArray[i].selected==true时,您可以向button1\u clickHandler添加一些代码。在代码中,执行SQL DELETE命令构造语句,如从某些表t中删除,其中t.firstNameField=+resultArray[i].firstName+和t.lastNameField=+resultArray[i].lastName。最好的做法可能是将某个recordId字段提取到resultArray。里德斯。大卫。