0

我在 flex 中有两个 Datagrid。我正在绑定数据Datagrid1Datagrid2喜欢

(Datagrid2.dataProvider=Datagrid1.dataProvider as ArrayCollection;) 

如果我删除Datagrid2数据然后自动另一个Datagrid1删除。

所以我不想删除Datagrid1. 我该怎么做,但是我还有其他选项可以删除数据Datagrid1

4

2 回答 2

1

这主要是因为 Datagrid1.dataProvider 和 Datagrid2.dataProvider 是指针。每次设置 object1 = object2 时,它们将共享相同的内存,因为你给它们相同的地址来查看。

删除此类引用的许多简单方法。

datagrid2.dataProvider = datagrid1.dataProvider.toArray();

或者

datagrid2.dataProvider = ObjectUtil.copy(datagrid1.dataProvider) as ArrayCOllection;

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[

            private function deleteFromDataGrid1():void {
                if(datagrid1.selectedIndex > -1)
                    myAC.removeItemAt(datagrid1.selectedIndex);

                myAC.refresh();
            }

            private function deleteFromDataGrid2():void {
                if(datagrid2.selectedIndex > -1)
                    datagrid2.dataProvider.removeItemAt(datagrid2.selectedIndex);
            }
        ]]>
    </mx:Script>

    <!-- An ArrayCollection with an array of objects -->
    <mx:ArrayCollection id="myAC">
        <!-- Use an mx:Array tag to associate an id with the array. --> 
        <mx:Array id="myArray">
            <mx:Object label="MI" data="Lansing"/>
            <mx:Object label="MO" data="Jefferson City"/>
            <mx:Object label="MA" data="Boston"/>
            <mx:Object label="MT" data="Helena"/>
            <mx:Object label="ME" data="Augusta"/>
            <mx:Object label="MS" data="Jackson"/>
            <mx:Object label="MN" data="Saint Paul"/>
        </mx:Array> 
    </mx:ArrayCollection>

    <mx:ApplicationControlBar>
        <mx:Button id="myButton" label="deleteFromDataGrid1" click="deleteFromDataGrid1()"/>
        <mx:Button id="myButton2" label="deleteFromDataGrid2" click="deleteFromDataGrid2()"/>
    </mx:ApplicationControlBar>

   <mx:HBox width="100%" horizontalAlign="center" height="100%">
     <mx:DataGrid id="datagrid1" dataProvider="{myAC}" height="100%"/>

    <mx:DataGrid id="datagrid2" dataProvider="{datagrid1.dataProvider.toArray()}" height="100%"/>
</mx:HBox>

</mx:Application>
于 2012-04-12T07:46:11.210 回答
0

使用以下...

(Datagrid2.dataProvider = ObjectUtil.copy(Datagrid1.dataProvider) as ArrayCollection;)

或者

private var _coll:ArrayCollection;

private function setData():void{
   Datagrid1.dataProvider =  _coll;
   Datagrid2.dataProvider =  ObjectUtil.copy(_coll) as ArrayCollection;
}

在上述情况下;如果您删除,从一个数据网格更改项目不会影响到另一个数据网格...

于 2012-04-12T07:00:47.687 回答