1 Reply Latest reply on Mar 27, 2011 12:35 AM by EvyatarBH

    DataGrid with dynamic columns & renderers

    EvyatarBH Level 3

      I'm developing using Flash Builder 4 & Flex SDK 4.1.

      I need to manage very dynamic DataGrid components and keep their definitions, which are all part of a complex item renderer of an Offers list.

       

      The objects structure is simplified as follows -

      Data: Model --> Offers ArrayCollection --> Offer VO --> DataGrid data ArrayCollection & DataGrid columns Array

       

      View: List --> Offer Item Renderer --> DataGrid

       

      1. Since the DataGrid's columns property accepts only an Array (not ArrayCollection), it seems like Data Binding for defining the columns is very problematic.

      I tried to bind it to the source property of an ArrayCollection that would keep my columns definitions, but it didn't really work (mainly header display bugs).

      What is the recommended way to keep the dynamic columns definition of a DataGrid?

       

      2. Each column can have a set of dynamic properties, so I created a "mutant" - Column VO that extends DataGridColumn and got a dynamic properties ArrayCollection on it.

      The columns got a custom header renderer that includes an icon when there are properties.

      The header renderers got 4 main states (NotSelectedWithProperties, SelectedWithProperties, NotSelectedWithoutProperties & SelectedWithoutProperties).

      However, the header renderer area seems a bit buggy when maintaning dynamic columns.

      Any thoughts on the subject?

       

      3. Anyway, I ended up recreating the DataGrid's columns Array very often (copying the columns definition on the offer's item renderer's dataChange event handler).

      Note that the dynamic properties can be edited when the column is selected and I copy their values from the view back to the model when entering the state NotSelectedWithProperties.

      This feels way too complicated and I really try to keep it simple, inspite of the required complexity.

      Does anyone have better ideas?

       

      4. In some cases the column's item renderer should also be modified into another DataGrid (grid-in-grid).

      I used the MXDataGridItemRenderer with a DataGrid and included an ArrayCollection for the "newValue" returned by the editor.

      (I use RendererIsEditor=true and on updateComplete populate that variable with the DataGrid's dataProvider contents)

      When needed, I loop though the data objects of the parent DataGrid and populate the related field with an ArrayCollection of key-value objects that are displayed on the internal DataGrid.

      After adding this feature I encounter very strange bugs -

       

      a. After editing the grid-in-grid values and changing the column's state (selecting & deselecting), I get the following exception:

      ArgumentError. Error #2025: The supplied DisplayObject must be a child of the caller.

      at flash.display::DisplayObjectContainer/setChildIndex()

      at mx.core::UIComponent/setChildIndex().......6993....

      All I could find about this is that it might be related to some context error or something, but I'm really stuck on this one.

       

      b. Sometimes another column might copy value from one row to another, running over the previous value.
      I'm not sure exactly what sequence of actions causes this behavior, but it's related to that itemRenderer for sure.

       

      c. Switching places with a column that uses the grid item renderer (headerShift) causes a stak overflow of StyleManager that tried to get style from the DataGridItemRenderer. This one I just found out, but couldn't reproduce a second time... strange!

       

      I'm pretty sure this caused another problem that I don't remember at the moment.

      The bottom line is that there got to be a better way to implement this feature within this already-complicated environment.

      Maybe I'm doing something very wrong here...

       

      Please advice and thanks for reading all this.

        • 1. Re: DataGrid with dynamic columns & renderers
          EvyatarBH Level 3

          Update on item 4a -

          This was a major issue (the main reason for opening this thread really) and I managed to resolve it!

           

          As part of my application, I override the default DataGrid behavior for column selection (headerRelease event).

          Instead of sorting, I change the column's header looks and define it as Selected (for showing its dynamic properties and enable its deletion).

          At first I did this by setting styles, but the look didn't refresh unless I created a new instance of the header renderer.

          Later I changed thi behavior to work with states, but I left the new header renderer instance creation commands and those lines created all the mess!

           

          Conclusion -

          If you define a custom header renderer for your datagrid column and then a custom item renderer, don't create a new instance of your header renderer!

           

          It would still be nice to get some response for the other issues I raised.

           

          Thanks and have a nice week.