ArrayCollection to populate Datagrid

Ask a Question related to Macromedia Flex General Discussion, Design and Development.

  1. #1

    Default ArrayCollection to populate Datagrid

    Hi,

    Im having problems getting my own array data to bind to a dataGrid. I've tried
    formating my array data several ways but none work. Here is an example of one
    of the iterations i have tried which i beleive should work. I build my array
    like this ...

    for(i) {
    arrayName.push( [{ fieldName1: value<i> , fieldName2: value<i>}] );
    }

    which gives me a nice 2d array. this is put in an arrayCollection which is
    bindable....

    arrayCol = new ArrayCollection( arrayName);

    and it is set as the dataProvider for my grid and the fieldNames are set as
    the column dataFields ...

    <mx:DataGrid x="424" y="425" height="125" width="340" id="grd" editable="true"
    enabled="true" dataProvider="{arrayCol}">
    <mx:columns>
    <mx:DataGridColumn headerText="Tag" dataField="fieldName1"
    editable="false"/>
    <mx:DataGridColumn headerText="Tag Value" dataField="fieldName1"
    editable="true"/>
    </mx:columns>
    </mx:DataGrid>

    I've spent a fair time trying to work this out and search the docs / this
    forum. If anyone has any recommendations about how to bind array /
    arrayCollection data to a dataGrid it would be greatly appreciated.

    Regards

    McDusty Guest

  2. Similar Questions and Discussions

    1. How to populate Advanced DataGrid
      <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"> <mx:Script> <! public var...
    2. populate textInput from DataGrid
      Please help! My DataGrid has "FIELD_NAME" and "FIELD_VALUES" columns. My TextInput objects has to match id names from "FIELD_NAME" columns, and...
    3. Populate Datagrid from Client side
      Hi All, I have a parent page. It will pop up a modal dialog (by calling show modal dialog) window. Dialog window will return a long string which...
    4. Datagrid doesnt populate :(
      Hello Im trying to do a simple page with data from my local sql server, and page just shows up blank...... I created a stored procedure with...
    5. How can I select items in a datagrid to populate a second datagrid.
      I currently have a datagrid that looks like the following. Sales Table Date Dogs Cats Fish 5/1/2004 4 ...
  3. #2

    Default Re: ArrayCollection to populate Datagrid

    The way you have it structured, it looks like you have a two-dimensional array
    where the second dimension always just contains a single item. Unless I'm
    misinterpreting your goal, the way to change this would be to remove the extra
    left/right brackets in your arrayName.push:

    change
    arrayName.push( [{ fieldName1: value<i> , fieldName2: value<i>}] );
    to
    arrayName.push( { fieldName1: value<i> , fieldName2: value<i>} );

    That way, you have a one-dimensional array, but each item in that array has a
    field named 'fieldName1' and another field named 'fieldName2'.

    Here is a complete sample that works for me (with the latest internal build --
    I haven't tried beta 3):

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

    public var mydata:Array;

    [Bindable]
    public var arrayCol:ArrayCollection;

    private function initData():void
    {
    mydata = [];
    mydata.push( { fieldName1: '1', fieldName2: '2' } );
    mydata.push( { fieldName1: '3', fieldName2: '4' } );

    arrayCol = new ArrayCollection(mydata);
    }
    ]]>
    </mx:Script>

    <mx:DataGrid x="424" y="425" height="125" width="340" id="grd" editable="true"
    enabled="true" dataProvider="{arrayCol}">
    <mx:columns>
    <mx:DataGridColumn headerText="Tag" dataField="fieldName1" editable="false"/>
    <mx:DataGridColumn headerText="Tag Value" dataField="fieldName2"
    editable="true"/>
    </mx:columns>
    </mx:DataGrid>

    </mx:Application>




    --
    Mike Morearty
    Developer, Flex Builder team
    [url]http://www.morearty.com/blog[/url]


    McDusty wrote:
    > Hi,
    >
    > Im having problems getting my own array data to bind to a dataGrid. I've tried
    > formating my array data several ways but none work. Here is an example of one
    > of the iterations i have tried which i beleive should work. I build my array
    > like this ...
    >
    > for(i) {
    > arrayName.push( [{ fieldName1: value<i> , fieldName2: value<i>}] );
    > }
    >
    > which gives me a nice 2d array. this is put in an arrayCollection which is
    > bindable....
    >
    > arrayCol = new ArrayCollection( arrayName);
    >
    > and it is set as the dataProvider for my grid and the fieldNames are set as
    > the column dataFields ...
    >
    > <mx:DataGrid x="424" y="425" height="125" width="340" id="grd" editable="true"
    > enabled="true" dataProvider="{arrayCol}">
    > <mx:columns>
    > <mx:DataGridColumn headerText="Tag" dataField="fieldName1"
    > editable="false"/>
    > <mx:DataGridColumn headerText="Tag Value" dataField="fieldName1"
    > editable="true"/>
    > </mx:columns>
    > </mx:DataGrid>
    >
    > I've spent a fair time trying to work this out and search the docs / this
    > forum. If anyone has any recommendations about how to bind array /
    > arrayCollection data to a dataGrid it would be greatly appreciated.
    >
    > Regards
    >
    Mike Morearty (Adobe) Guest

  4. #3

    Default Re: ArrayCollection to populate Datagrid

    cheers for your response Mike.

    It works a treat!
    McDusty Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139