1 Reply Latest reply on Feb 28, 2008 2:21 PM by ericbelair

    About how to construct dynamic DataGrid

    Narek_Saribekyan
      Hi All,
      Can someone help me with this?

      Note: Here attribute is standing for database fields (columns).
      My task is to represent some part of database. The fields are chosen by user and only chosen fields from database have to be displayed in DataGrid control.

      Flex DataGrid control does not provide dynamically constructed tables. More clear, DataGrid control requires a data provider object (which must be a collection) consisting of objects having fields (each field is associated with one attribute in our case, and so each object in data provider represents one row in DataGrid). The problem is that I don't know how to build such objects (I know the objects structure only when I receive result from web service). Of course I can build objects that contain all attributes, then I'll choose which one of them to display, but we have too many attributes.

      Thanks in advance,
      Narek
        • 1. Re: About how to construct dynamic DataGrid
          ericbelair Level 1
          I do something similar in my app.

          I have a DataGrid MXML Component. I pass an ArrayCollection to the Component with data that defines what columns to display in the DataGrid, and how to display them. Then I call a function in the Component to loop over that Collection, building an Array of DataGridColumn objects. Here is part of that function:

          // Loop through each object in the Display Columns Array Collection
          for each (var:object:Object in displayColumns)
          {
          // Create a new DataGrid Column
          var dataGridColumn:DataGridColumn = new DataGridColumn();

          // Set the column properties
          dataGridColumn.dataField = object.DataField;
          dataGridColumn.headerText = object.HeaderText;
          dataGridColumn.sortable = object.Sortable;
          dataGridColumn.draggable = object.Draggable;

          // Add the column to the temporary columns Array
          columns.push(dataGridColumn);
          }

          // Update the columns Array of the DataGrid
          dataGrid.columns = columns;

          I do a lot more with itemRenderers and sortCompareFunctions, but this is the basics of what I do.

          Hope that helps.