3 Replies Latest reply on Apr 7, 2009 7:40 AM by webvalue

    How to make a copy of an instance of a class?

    webvalue Level 1

      Hi,

       

      If I create a somethingk like a datagrid and filled it with data during runtime, and then I would like to display the datagrid in another place at the same time. So how to make a copy of the datagrid with the data filled?

       

      Thanks.

        • 1. Re: How to make a copy of an instance of a class?
          matthew horn Level 3

          Something like this might help:

          http://goonquest.com/blog/?tag=flex-3

           

          Basically uses the createComponentFromDescriptor() method to create a clone.

           

          hth,

          matt horn

          flex docs

          • 2. Re: How to make a copy of an instance of a class?
            FlashGen

            Hi webvalue, do you wish to display the same data that this DataGrid is using? If so just create a new DataGrid instance and give it the same data as its dataProvider. For example the code below uses two DataGrids that both consume the same data:

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                            layout="absolute"
                            applicationComplete="init(event)">
                <mx:Script>
                    <![CDATA[
                   
                        [Bindable]
                        private var _dp        :Array;
                       
                        private function init(e:Event):void
                        {
                            _dp = [
                                    {label:"FlashGen.Com", data:"http://www.flashgen.com"},
                                    {label:"Adobe", data:"http://www.adobe.com"},
                                    {label:"Flex.org", data:"http://www.flex.org"},
                                    ]
                        }
                    ]]>
                </mx:Script>
                <mx:Panel x="10" y="10" width="600" height="207" layout="absolute" title="Multiple DataGrids using the same data source">
                    <mx:DataGrid id="dg2" dataProvider="{_dp}"  left="0" top="0" bottom="0" width="50%"/>
                    <mx:DataGrid id="dg1" dataProvider="{_dp}" right="0" bottom="0" width="50%" top="0"/>
                </mx:Panel>
            </mx:Application>

             

            Cloning a DataGrid component doesn't seem to be the right solution given the information you have provided. If you wanted to reuse the DataGrid in a separate view you could always reparent it to the new view by removing it from the current views displaylist and adding it to the new views displaylist like the example below illustrates, here I'm moving the DataGrid (dg) from one Panel component (dghost) to another (dghost2):


            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                            layout="absolute"
                            applicationComplete="init(event)">
                <mx:Script>
                    <![CDATA[
                   
                        [Bindable]
                        private var _dp        :Array;
                       
                        private function init(e:Event):void
                        {
                            _dp = [
                                    {label:"FlashGen.Com", data:"http://www.flashgen.com"},
                                    {label:"Adobe", data:"http://www.adobe.com"},
                                    {label:"Flex.org", data:"http://www.flex.org"},
                                    ]
                        }
                       
                        private function moveDataGrid(e:MouseEvent):void
                        {
                            dghost.removeChild(dg);
                            dghost2.addChild(dg);
                        }
                    ]]>
                </mx:Script>
                <mx:Panel id="dghost" x="10" y="10" width="300" height="207" layout="absolute" title="DataGrids original parent container">
                    <mx:DataGrid id="dg" dataProvider="{_dp}" right="0" bottom="0" top="0" left="0"/>
                    <mx:ControlBar>
                        <mx:Button label="Move DataGrid" click="moveDataGrid(event)"/>
                    </mx:ControlBar>
                </mx:Panel>
                <mx:Panel id="dghost2" x="10" y="225" width="300" height="207" layout="absolute" title="DataGrids original parent container">
                </mx:Panel>
            </mx:Application>

             

            While you could clone the DataGrid, when dealing with display objects it's often easier to reparent them as you are only moving it from one view to another; this has the added benefit of saving on memory as you still only have the original instance of the DataGrid not that instance and a new "cloned" instance.

             

            I hope that helps provide additional options beyond just cloning

             

            -
            regards
            m

             

            -----------------------------------------------
            Mike Jones
            FlashGen.Com

             

            Adobe Community Expert For Flex
            Adobe Certified Expert for Flex 3 & AIR
            -----------------------------------------------
            w./ www.flashgen.com

            b./ blog.flashgen.com

            ----------------------------------------------

            Catch Me At

             

            Scotch on the Rocks London '09
            Flash on the Beach Brighton '09
            -----------------------------------------------
            Author Of Developing Flex Components
            Addison-Wesley (Q4 / 2009)
            -----------------------------------------------

            • 3. Re: How to make a copy of an instance of a class?
              webvalue Level 1

              Hi Mike,

               

              While I will still be interested in pursuing the cloning method, your 2 solutions just work for me this time. Thanks a lot for your explanations.