4 Replies Latest reply on Aug 23, 2008 3:49 PM by Sebastien Arbogast

    Configuring columns in an AdvancedDataGrid programatically

    Sebastien Arbogast
      I'm trying to configure an AdvancedDataGrid so that grouped columns correspond to the following configuration:

      private var fieldCategories:Array = [
      new FieldCategory("profils.identifier", [
      new Field("profils.firstname", "firstname", FieldType.STRING),
      new Field("profils.lastname", "lastname", FieldType.STRING)
      ]),
      new FieldCategory("profils.contact", [
      new Field("profils.private_email", "email_priv", FieldType.STRING),
      new Field("profils.private_phone", "tel_priv", FieldType.STRING)
      ])
      ];

      Where FieldCategory, Field and FieldType are custom classes of my own. So on creationComplete of the parent container of my AdvancedDataGrid, I run the following init() method:

      public function init():void{
      initColumns();
      service.loadMembers();
      }

      public function initColumns():void{
      var columnGroups:ArrayCollection = new ArrayCollection();
      for each(var fieldCategory:FieldCategory in fieldCategories){
      var columnGroup:AdvancedDataGridColumnGroup = new AdvancedDataGridColumnGroup(ResourceManager.getInstance().getString('messages', fieldCategory.nameKey));
      var columns:ArrayCollection = new ArrayCollection();
      for each(var field:Field in fieldCategory.fields){
      var column:AdvancedDataGridColumn = new AdvancedDataGridColumn();
      column.headerText = ResourceManager.getInstance().getString('messages', field.nameKey);
      column.dataField = field.dataField;
      columns.addItem(column);
      }
      columnGroup.children = columns.source;
      columnGroups.addItem(columnGroup);
      }
      userDataGrid.groupedColumns = columnGroups.source;
      }

      And service.loadMembers() calls a RemoteObject method whose result handler is the following:

      public function resultHandler(event:ResultEvent):void{
      userDataGrid.dataProvider = (ArrayCollection)(event.result);
      }

      My remote object returns 50 Objects and each Object has a 'firstname' field amongst others. I know that records are loaded and the dataProvider loads it because 50 lines are created in the data grid. Yet, there is nothing displayed in the cells, as if column.dataField was not correctly assigned.
      Any idea on what I may have done wrong?
        • 1. Re: Configuring columns in an AdvancedDataGrid programatically
          Level 7

          "Sebastien Arbogast" <webforumsuser@macromedia.com> wrote in message
          news:g8os6r$6rh$1@forums.macromedia.com...
          > I'm trying to configure an AdvancedDataGrid so that grouped columns
          > correspond
          > to the following configuration:
          >
          > private var fieldCategories:Array = [
          > new FieldCategory("profils.identifier", [
          > new Field("profils.firstname", "firstname", FieldType.STRING),
          > new Field("profils.lastname", "lastname", FieldType.STRING)
          > ]),
          > new FieldCategory("profils.contact", [
          > new Field("profils.private_email", "email_priv", FieldType.STRING),
          > new Field("profils.private_phone", "tel_priv", FieldType.STRING)
          > ])
          > ];

          What's the code for your Field class?

          -Amy


          • 2. Re: Configuring columns in an AdvancedDataGrid programatically
            Sebastien Arbogast Level 1
            Here it is:

            public class Field
            {
            private var _dataField:String;
            public function get dataField():String{return _dataField;}

            private var _nameKey:String;
            public function get nameKey():String{return _nameKey;}

            private var _type:FieldType;
            public function get type():FieldType{return _type;}

            public function Field(nameKey:String, dataField:String, type:FieldType)
            {
            this._nameKey = nameKey;
            this._type = type;
            this._dataField = dataField;
            }

            }
            • 3. Re: Configuring columns in an AdvancedDataGrid programatically
              Level 7

              "Sebastien Arbogast" <webforumsuser@macromedia.com> wrote in message
              news:g8pa07$op1$1@forums.macromedia.com...
              > Here it is:
              >
              > public class Field
              > {
              > private var _dataField:String;
              > public function get dataField():String{return _dataField;}
              >
              > private var _nameKey:String;
              > public function get nameKey():String{return _nameKey;}
              >
              > private var _type:FieldType;
              > public function get type():FieldType{return _type;}
              >
              > public function Field(nameKey:String, dataField:String, type:FieldType)
              > {
              > this._nameKey = nameKey;
              > this._type = type;
              > this._dataField = dataField;
              > }
              >
              > }

              I am curious as to why you're using an ArrayCollection to populate your
              columns, then just pulling out the array out of it at the end. I don't
              _think_ that should be causing a problem, but hard to say. One thing that
              looks wrong to me is that your nameKey is a string containing dot notation.
              This looks like maybe you're expecting something, somewhere to be able to
              execute that as if it were a real object/property. You probably know
              exactly what you're doing, but I thought I'd point it out, because I've made
              that mistake myself.

              But if that's not it, I'd try setting a breakpoint and inspecting the value
              of your columns collction.

              HTH;

              Amy


              • 4. Re: Configuring columns in an AdvancedDataGrid programatically
                Sebastien Arbogast Level 1
                The dot notation in my resource keys is just a way to create some hierarchy among them, an old habit from my Java experience.
                As for the ArrayCollection, I'm using it because it grows automatically, but maybe there's a way to do it directly with an array. I miss some experience with actionscript to know.

                Actually, I managed to get it working by using the "do-it-all-over-again" approach but I don't know why it works better now than before. I just changed the algorithm of initColumns:

                public function initColumns():void{
                var columnGroups:ArrayCollection;
                var columns:ArrayCollection;
                var columnGroup:AdvancedDataGridColumnGroup;
                var column:AdvancedDataGridColumn;

                columnGroups = new ArrayCollection();
                for each(var fieldCategory:FieldCategory in fieldCategories){
                columnGroup = new AdvancedDataGridColumnGroup();
                columnGroups.addItem(columnGroup);
                columnGroup.headerText = ResourceManager.getInstance().getString("messages", fieldCategory.nameKey);
                columns = new ArrayCollection();
                for each(var field:Field in fieldCategory.fields){
                column = new AdvancedDataGridColumn();
                columns.addItem(column);
                column.headerText = ResourceManager.getInstance().getString("messages", field.nameKey);
                column.dataField = field.dataField;
                }
                columnGroup.children = columns.source;
                }
                userDataGrid.groupedColumns = columnGroups.source;
                }