6 Replies Latest reply on Sep 22, 2008 7:41 AM by Wilhnsn

    Using DataGrid on Unusual Data Structures

    Wilhnsn
      I have a class that looks like this (see listing 1) and I want the DataGrid to access certain properties of the orders array (which is an associative array). The problem is, there doesn't seem to be a way to access the data beyond the first-level property (i.e. it can't access a property of a property) for the dataField of DataGridColumn so I made a renderer custom renderer (see listing 2)


      Then, I intercepted the itemEdit event of the datagrid to cancel the default behavior (which will result in an error since DataGrid will attempt to do an assignment to an invalid property.) It works kinda ok except that the "tab" key which would let the user navigate between cells isn't working. In other words, I let the renderer handle all the logic and such.

      I know that my method is very hacky so I would like to know if there's any alternative to what I'm doing. Another method I thought of is to make a factory producing dynamic objects which would automagically translate data.orderName = quantity; to data.orders[orderName] = quantity. or something, but that's still hacky.
        • 1. Re: Using DataGrid on Unusual Data Structures
          Level 7

          "Wilhnsn" <webforumsuser@macromedia.com> wrote in message
          news:gaqno4$dvo$1@forums.macromedia.com...
          >I have a class that looks like this (see listing 1) and I want the DataGrid
          >to
          > access certain properties of the orders array (which is an associative
          > array).
          > The problem is, there doesn't seem to be a way to access the data beyond
          > the
          > first-level property (i.e. it can't access a property of a property) for
          > the
          > dataField of DataGridColumn so I made a renderer custom renderer (see
          > listing 2)
          >
          >
          > Then, I intercepted the itemEdit event of the datagrid to cancel the
          > default
          > behavior (which will result in an error since DataGrid will attempt to do
          > an
          > assignment to an invalid property.) It works kinda ok except that the
          > "tab" key
          > which would let the user navigate between cells isn't working.
          >
          > I know that my method is very hacky so I would like to know if there's any
          > alternative to what I'm doing. Another method I thought of is to make a
          > factory
          > producing dynamic objects which would automagically translate
          > data.orderName =
          > quantity; to data.orders[orderName] = quantity. or something, but that's
          > still
          > hacky.
          >
          > Listing 1:
          > class MyDS {
          > [Bindable[
          > public var name:String;
          > [Bindable]
          > public var orders:Array;
          > }
          >
          > Listing 2:
          > <?xml version="1.0" encoding="utf-8"?>
          > <mx:HBox xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%">
          > <mx:TextInput width="100%" id="tiInput" text="{data.orders[orderName]}"
          > change="onChange()" editable="true"/>
          > <mx:Script>
          > <![CDATA[
          > [Bindable]
          > public var orderName:String;
          >
          > public var quantity:uint;
          >
          > private function onChange() : void {
          > quantity = Number(tiInput.text);
          > data.orders[orderName] = quantity;
          > }
          >
          > ]]>
          > </mx:Script>
          > </mx:HBox>

          You might want to change your approach a bit
          http://blog.tsclausing.com/post/20


          • 2. Re: Using DataGrid on Unusual Data Structures
            Wilhnsn Level 1
            I don't think that would solve the problem. Actually, the DataGrid's 1st column is made up of the names of the customers, and the rest of the column are the products. So the cell in the grid contains how many products a certain customer has ordered (and the user could quickly modify each cell as if it was an Excel spreadsheet). If I collapse it into a grouped view, I would end up having a long and narrow list which looks very unbalanced.
            • 3. Re: Using DataGrid on Unusual Data Structures
              ntsiii Level 3
              labelFunction will work for displaying data from anywhere within an item objects structure.

              If you need to edit such data, you could either us a custom renderer and let do all of the data manipulation (set redererIsEditor="false"), or add properties to your value object.

              Actually I am not quite following what you want. You refer to DataGrid but do not have one in your code.

              Tracy
              • 4. Re: Using DataGrid on Unusual Data Structures
                Wilhnsn Level 1
                Sorry about that. Let me reword the situation. This is a different one but follows the same concept:
                Imagine a standard RPG where each unit has an element (i.e. fire, ice, water, etc) and when one element attacks another, there's this damage multiplier ratio applied to the base damage. Then you make a table with the first row having the names of the "recievers" of the attack and the first column containing the names of the attackers. So, a cell in that table contains the damage modifier of the attacker to the reciever.

                Each row in the table is represented by a class UnitElement (see listing 0) which contains an associative array of UnitElement -> Number. So each entry in that associative array contains the damage modifier if "this" instance attacks the key in the associative array (note that The associative array is actually sparse and defaults to 1 when the entry is missing).

                Now, I want that table to be edtiable and when a class is added, a new row and column is automatically added. My main problem is having each cell editable. If they're static, I can just use DataGridColumn.labelFunction but since they're not... well... look at listing 2 and 3 to see how I "tried" to implement the renderer. The problem is, whenever a cell is finish editing, an Error props out at the itemEditEnd event saying that my renderer doesn't contain a "text" field. When I add it, another error props out saying that the UnitElement.x (where x is the label of the column) does not exist because it tries to grab the value from renderer.text and assigns it to UnitElement.x. This is not what I want to happen.
                When I tried to intercept that itemEndEvent and cancel the default behavior... well, it doesn't try to do things on its own however, the tab navigation doesn't work (i.e. pressing tab doesn't allow you to switch cells).
                • 5. Re: Using DataGrid on Unusual Data Structures
                  Level 7

                  "Wilhnsn" <webforumsuser@macromedia.com> wrote in message
                  news:gb719g$hvr$1@forums.macromedia.com...
                  > Sorry about that. Let me reword the situation. This is a different one but
                  > follows the same concept:
                  > Imagine a standard RPG where each unit has an element (i.e. fire, ice,
                  > water,
                  > etc) and when one element attacks another, there's this damage multiplier
                  > ratio
                  > applied to the base damage. Then you make a table with the first row
                  > having the
                  > names of the "recievers" of the attack and the first column containing the
                  > names of the attackers. So, a cell in that table contains the damage
                  > modifier
                  > of the attacker to the reciever.
                  >
                  > Each row in the table is represented by a class UnitElement (see listing
                  > 0)
                  > which contains an associative array of UnitElement -> Number. So each
                  > entry in
                  > that associative array contains the damage modifier if "this" instance
                  > attacks
                  > the key in the associative array (note that The associative array is
                  > actually
                  > sparse and defaults to 1 when the entry is missing).
                  >
                  > Now, I want that table to be edtiable and when a class is added, a new row
                  > and
                  > column is automatically added. My main problem is having each cell
                  > editable. If
                  > they're static, I can just use DataGridColumn.labelFunction but since
                  > they're
                  > not... well... look at listing 2 and 3 to see how I "tried" to implement
                  > the
                  > renderer. The problem is, whenever a cell is finish editing, an Error
                  > props out
                  > at the itemEditEnd event saying that my renderer doesn't contain a "text"
                  > field. When I add it, another error props out saying that the
                  > UnitElement.x
                  > (where x is the label of the column) does not exist because it tries to
                  > grab
                  > the value from renderer.text and assigns it to UnitElement.x. This is not
                  > what
                  > I want to happen.
                  > When I tried to intercept that itemEndEvent and cancel the default
                  > behavior...
                  > well, it doesn't try to do things on its own however, the tab navigation
                  > doesn't work (i.e. pressing tab doesn't allow you to switch cells).

                  You'll have to forgive me...it's late and my eyes are crossing. I looked to
                  see if you had rendererIsEditor="false" anywhere and I didn't see it. If
                  you already have that, disregard. But if not, you need to use this if you
                  want a separate item renderer and editor.



                  • 6. Re: Using DataGrid on Unusual Data Structures
                    Wilhnsn Level 1
                    Oh, that's alright. Anyway, when I set rendererIsEditor to false, whenever I click on a cell for the first time, UnitElement.toString() gets called (which outputs to "[object UnitElement]" since I didn't define toString() ) and it still attempts to access UnitElement.x (where x is the target UnitElement name... like UnitElement.fire)