3 Replies Latest reply on Dec 13, 2006 2:40 AM by M. Huisman

    DataGrid Control using ActionScript

    M. Huisman Level 1
      Hi,

      Is it possible to generate the rows of a datagrid dynamically using actionscript?

      I want to put information of multiple XML's on one line of my datagrid.

      Mark
        • 2. Re: DataGrid Control using ActionScript
          peterent Level 2
          The DataGrid is just the viewer of its dataProvider (eg, an ArrayCollection). So to add rows dynamically to the DataGrid you would actually add records to the dataProvider and the DataGrid will show them.

          First thing is to decide how you want the DataGrid to look. For example, do you want the DataGrid to show only 1 row at a time and then add new rows until, say, 10 rows are visible, then all new rows are accessible through a scrollbar? Or do you want to fix the DataGrid at showing 10 rows and just fill them, adding new rows when the dataProvider goes beyond 10 records?

          var dp:ArrayCollection = new ArrayCollection(); // empty dataProvider
          ...
          <mx:DataGrid dataProvider="{dp}" ... /> <!-- dynamically bind dataProvider -->

          Now add a new record whenever you need to: dp.addItem( newrecord );

          If you do not specify DataGridColumns when creating the DataGrid, the first record added to the dataProvider will establish the columns and name them using the field names in the record. Any records thereafter with additional fields will not get new columns. For that case you need to programmatically add DataGridColumns.
          • 3. Re: DataGrid Control using ActionScript
            M. Huisman Level 1
            Ok, I found out my problem, the thing is i wanted to combine info in a datagrid, from 2 xml's, but since it's not directly possible to access a row of the grid, I created a labelfunction for the datagridcolumns which i wanted to populate with a different value.

            Source is included below, hope it will help someone.

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="1120" height="300" initialize="doInit();">
            <mx:XML id="dataContacts" source="/data/Contactperson.xml"/>
            <mx:XML id="dataCommunication" source="/data/Communication.xml"/>
            <mx:Script>
            <![CDATA[
            private function doInit():void {
            trace(dataContacts.Contactperson.Firstname);
            }
            protected function getPhonenoById(row:Object, column:DataGridColumn):String {
            for each(var node:XML in row.Communicationid) {
            var value:String = new String;
            if (dataCommunication.Communications.Communication.@id == node.toString) {
            value = dataCommunication.Communication.(@id == node.toString()).(@type == "Phone");
            if (value != "") // if the string is empty
            {
            // return to the returning parameter.
            break;
            }
            }
            }
            // returning the value to the cell
            return value;
            }
            ]]>
            </mx:Script>
            <mx:DataGrid x="10" y="10" width="493" id="dg" dataProvider="{dataContacts.Contactperson}">
            <mx:columns>
            <mx:DataGridColumn id="dcFirstname" dataField="Firstname" headerText="Voornaam"/>
            <mx:DataGridColumn id="dcLastname" dataField="Lastname" headerText="Achternaam"/>
            <mx:DataGridColumn id="dcPhone" dataField="Communicationid" labelFunction="getPhonenoById" headerText="Telefoonnummer"/>
            </mx:columns>
            </mx:DataGrid>
            </mx:Canvas>