5 Replies Latest reply on Dec 7, 2006 7:22 PM by ntsiii

    DataGridColumns labelFunction

    michael_ramirez44 Level 1
      I have a labelFunction attached to my dataGridColumns.

      newColumn.labelFunction = columnTextFunction;

      If I use the labelFunction below I get weird and random behavior with my Flex applications. Controls don't functions correctly and render with overlapping text.

      private function columnTextFunction(item:Object,column:DataGridColumn):String
      {
      return item[column.dataField].toString();
      }

      If I change my labelFunction to the one below everything works fine.All I removed was the toString() method call.

      private function columnTextFunction(item:Object,column:DataGridColumn):String
      {
      return item[column.dataField];
      }

      Any thoughts :(
        • 1. Re: DataGridColumns labelFunction
          dimival Level 1
          why are you doing this:
          item[column.dataField] ???

          Are you using the same function for every column? I would'nt do that
          • 2. Re: DataGridColumns labelFunction
            michael_ramirez44 Level 1
            item[column.dataField] returns the text that should display in the column. Basically it returns the correct value from my data provider. I am not using the same function for every column it's only used on two columns.
            • 3. DataGridColumns labelFunction
              dimival Level 1
              Ok i think you are confused of when to use labelFunction.

              When you declare a DataGridColumn you can use the dataField property to indicate the column which item's property to display.
              You use labelFunction when you need the column to display something different of what the item contains, for example: The item has a boolean property and instead of displaying true or false you want it to display Yes or No, you will do something like this:


              public function showYesNo(item: Object, column: DataGridColumn) : String
              {
              if (item.propertyName == true) {
              return 'Yes';
              }
              return 'No'
              }

              You shouldn't use dataField and labelFunction together, use only one, dataField when you want to display a property of the object in that column OR labelFunction when you want to display something different of what the object properties contain

              So in your application you either do this:

              newColumn.dataField = propertyName;

              or do this:

              newColumn.labelFunction = columnTextFunction;

              private function columnTextFunction(item:Object,column:DataGridColumn):String
              {
              return item.propertyName.toString( ) // use toString only if your property isn't a String
              }

              newColumn.labelFunction = columnTextFunction;
              • 4. Re: DataGridColumns labelFunction
                michael_ramirez44 Level 1
                I need to use the labelFunction on the dataField because my datagrid is created dynamically. I don't know what columns I will be creating until runtime. I use the label function to format my columns.

                private function formatColumnNumber(item:Object,column:DataGridColumn):String
                {
                return numFormatter.format(item[column.dataField]);
                }

                I understand that this is not the normal way of using the labelFunction and dataField. I think we are getting away from my real question. Why does Flex misbehave once I add the toSting() method to item[column.dataField]?
                • 5. Re: DataGridColumns labelFunction
                  ntsiii Level 3
                  item[column.dataField] is fine. It is exactly why the column argument is provided.

                  Are you just curious why toString() causes problems? There are some things to try if you just want to investigate and have the time. It sounds like you have a solution.

                  Tracy