7 Replies Latest reply on Aug 12, 2008 6:23 AM by anoweb

    DataGrid ItemEditor - bound to data, unique for each row

    anoweb Level 1
      I have a DataGrid with three columns (type, kind, amount). The type and kind columns each have a ComboBox as an item editor. When a type is selected the data that is bound to the "kind" combobox changes (based on the type chosen). Imagine this is based on foods.

      var types:Array = ['Fruit', 'Vegetables'];
      var kinds:Array = [];
      var fruits:Array = ['Banana', 'Orange', 'Apple'];
      var vegetables:Array = ['Corn', 'Broccoli', 'Green Beans'];

      The combobox editors look like:

      <mx:Component id="comboEditorTypes">
      <mx:ComboBox dataProvider="{types}" change="onDataSourcesChange(event)" />
      </mx:Component>

      <mx:Component id="comboEditorKinds">
      <mx:ComboBox dataProvider="{kinds}" />
      </mx:Component>

      The event handler for the "types" combobox does this:

      private function onDataSourcesChange(e:ListEvent):void {
      var index:int = (e.target as ComboBox).selectedIndex;
      if (index == 0) {
      kinds = fruits;
      }
      else {
      kinds = vegetables;
      }
      }


      So this works however, when there are multiple rows in the DataGrid and you change the "Type" in one row, it changes the options in the comboboxes in the "kind" column in all of the other rows.

      Any suggestions on how to do this? I basically need each row to only know about its own state.
        • 1. Re: DataGrid ItemEditor - bound to data, unique for each row
          amthekkel Level 1
          hi,

          I am fairly new to flex as well but I think the problem is that in your onDataSourcesChange function you are not specifying which Kind row combobox to be update and that is why its updating all combo boxes in the kind column.
          best bet would be to look at accessing the specified row items. try this might work, e.g. if ur datagrid is called "Datagrid"

          then in your onDataSourcesChange function, try the following
          ....
          if(index==0)
          {
          DataGrid.dataProvider.getItemAt(DataGrid.selectedIndex).Kind = fruits;
          }

          the left hand side provides the value of the Kind Column data for the selected row.

          if it doesn't work, then look into ways of obtaining setting item values at a particular cell. Might need to modify it to work with item editors.
          • 2. Re: DataGrid ItemEditor - bound to data, unique for each row
            anoweb Level 1
            I think I need to somehow get the actual item editor for that cell. What does ".Kind" represent, is that just the "id" of the "kind" column?
            • 3. Re: DataGrid ItemEditor - bound to data, unique for each row
              amthekkel Level 1
              .kind was referring to the datafield property of the datagrid column.

              I used it to access the value at the specified cell ie in my case i was retrieving values at the location rather than setting it,but i would presume that you might be able to modify it to set the value at that point as well.
              probably instead of using getItemAt, usign the setItemAt method might work..

              see if this link is of any use: http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDo cs_Parts&file=00000509.html

              cheers
              abhi
              • 4. Re: DataGrid ItemEditor - bound to data, unique for each row
                anoweb Level 1
                The problem is I need to change the data being displayed in the combobox in the "kind" field.
                • 5. Re: DataGrid ItemEditor - bound to data, unique for each row
                  Level 7

                  "anoweb" <webforumsuser@macromedia.com> wrote in message
                  news:g7rr3c$2se$1@forums.macromedia.com...
                  >I think I need to somehow get the actual item editor for that cell. What
                  >does ".Kind" represent, is that just the "id" of the "kind" column?

                  You're going about it from the wrong direction. Your combobox needs to be
                  able to look at the data and/or listData for the row and react. There is no
                  way that you can set one variable globally and bind a bunch of different
                  comboboxes to it and expect them to somehow have different values.


                  • 6. Re: DataGrid ItemEditor - bound to data, unique for each row
                    Level 7

                    "amthekkel" <webforumsuser@macromedia.com> wrote in message
                    news:g7rq31$1q1$1@forums.macromedia.com...
                    > hi,
                    >
                    > I am fairly new to flex as well but I think the problem is that in your
                    > onDataSourcesChange function you are not specifying which Kind row
                    > combobox to
                    > be update and that is why its updating all combo boxes in the kind column.
                    > best bet would be to look at accessing the specified row items. try this
                    > might
                    > work, e.g. if ur datagrid is called "Datagrid"
                    >
                    > then in your onDataSourcesChange function, try the following
                    > ....
                    > if(index==0)
                    > {
                    > DataGrid.dataProvider.getItemAt(DataGrid.selectedIndex).Kind = fruits;
                    > }
                    >
                    > the left hand side provides the value of the Kind Column data for the
                    > selected
                    > row.
                    >
                    > if it doesn't work, then look into ways of obtaining setting item values
                    > at a
                    > particular cell. Might need to modify it to work with item editors.
                    >
                    You're close. The data and listData variables for each row will contain
                    information that is relevant to all columns in that row.


                    • 7. Re: DataGrid ItemEditor - bound to data, unique for each row
                      anoweb Level 1
                      Yea I see. I have a subclass of DataGrid and in there I am overriding the "createItemEditor". I then have a public property on my DataGrid "itemEditorCreated" that gets called back, something like:

                      public override function createItemEditor(colIndex:int, rowIndex:int):void {
                      super.createItemEditor(colIndex, rowIndex);

                      if (itemEditorCreatedFunc != null) {
                      itemEditorCreatedFunc(this, colIndex, rowIndex);
                      }
                      }

                      I then let the creator of the grid handle checking which editor was being created. If the editor is being created for the "Kind" column, I do a simple check of the value in the "type" column for that row and then set the "dataProvider" on the editor for the "Kind" column.