Sabha B
Sabha B

Reputation: 2089

Flex datagrid and nested Array dataprovider

I have used datagrid on many projects populate the grid using the following "Usual data structure" and brought the standard o/p as shown below Image. But now for one assignment I want to bring the same grid result using the below mentioned "Complex data structure" (nested array). I have some Idea which is process the data before pushing it to the Grid but the problem am having is I need to perform some update , edit delete operation through the grid renderers and the same should be reflected into the source collection also. Please let me know is there a way I can use the "Complex structure" and bring the expected o/p using any flex in build properties. Thanks in Advance.

Usual data structure

steps = [a,b,c];
a = {x:100,y:y1,z:z1};
b = {x:200,y:y2,z:z2};
c = {x:300,y:y3,z:z3};

Complex data structure

[] is an Array collection not Array type.    
a = [100,y1,z1];
b = [200,y2,z2];
c = [300,y3,z3];
steps = [[a,some objects],[b,some objects],[c,some objects]];

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
    <![CDATA[

        import mx.collections.ArrayCollection;

        private var a:ArrayCollection = new ArrayCollection(['x1','y1','z1']);
        private var b:ArrayCollection = new ArrayCollection(['x2','y2','z2']);
        private var c:ArrayCollection = new ArrayCollection(['x3','y3','z3']);
        [Bindable]
        private var stepsObjs:ArrayCollection = new ArrayCollection([{ items: a},{ items: b},{ items: c}]);

    ]]>
</mx:Script>

<mx:DataGrid dataProvider="{stepsObjs}" >
    <mx:columns>
        <mx:DataGridColumn dataField="items.0" headerText="x" />
        <mx:DataGridColumn dataField="items.1" headerText="y" />
        <mx:DataGridColumn dataField="items.2" headerText="z" />
    </mx:columns>
</mx:DataGrid>  
</mx:Application>

Upvotes: 0

Views: 3669

Answers (2)

Jacob Eggers
Jacob Eggers

Reputation: 9322

EDIT Replacing with code to solve the new question.


This one worked for me:

<?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;

            private var a:ArrayCollection = new ArrayCollection([100,'y1','z1']);
            private var b:ArrayCollection = new ArrayCollection([200,'y2','z2']);
            private var c:ArrayCollection = new ArrayCollection([300,'y3','z3']);

            private var stepsObjs:ArrayCollection = new ArrayCollection([{ items: a},{ items: b},{ items: c}]);
            private var stepsColl:ArrayCollection = new ArrayCollection([[a],[b],[c]]);

        ]]>
    </fx:Script>
    <s:layout>
        <s:VerticalLayout />
    </s:layout>
    <mx:DataGrid dataProvider="{stepsObjs}" >
        <mx:columns>
            <mx:DataGridColumn dataField="items.0" headerText="x" />
            <mx:DataGridColumn dataField="items.1" headerText="y" />
            <mx:DataGridColumn dataField="items.2" headerText="z" />
        </mx:columns>
    </mx:DataGrid>

    <mx:DataGrid dataProvider="{stepsColl}" >
        <mx:columns>
            <mx:DataGridColumn dataField="0.0" headerText="x" />
            <mx:DataGridColumn dataField="0.1" headerText="y" />
            <mx:DataGridColumn dataField="0.2" headerText="z" />
        </mx:columns>
    </mx:DataGrid>

</s:Application>

Upvotes: 3

Amy Blankenship
Amy Blankenship

Reputation: 6961

The itemRenderers should dispatch an event that describes what should happen, and then it should be handled higher up.

Like this:

//inside item renderer
dispatchEvent(new ItemEvent(ItemEvent.DELETE_ITEM, true, item));//where true tells the event to bubble (you'll need to create this event)

//somewhere above the DataGrid
dataGrid.addEventListener(ItemEvent.DELETE_ITEM, deleteItemFromSource);

protected function deleteItemFromSource(e:ItemEvent):void {
    var lcv:ListCollectionView = (dataGrid.dataProvider as ListCollectionView);
    lcv.removeItemAt(lcv.getItemIndex(e.item));
}

Note as a FYI that you should be using some sort of ListCollectionView if you want the datagrid to automatically update when you change it, not an Array.

HTH;

Amy

Upvotes: 1

Related Questions