3 Replies Latest reply on Feb 12, 2008 12:02 PM by TAB68

    LabelFunction called twice for first row of DataGrid

    TAB68
      Hi,

      I'm fairly new to Flex and have noticed some strange behaviour with LabelFunction when used with a DataGridColumn.

      I have a DataGrid with simple XML passed in through the dataprovider. I call a LabelFunction on a column and found that the output from the function was misformatted for the first row in the column. I then put a trace in the function and found it was being called twice for the first row.

      I can't find any info on why this is happening or even if anyone else has seen this and am wondering if this is something I'm misunderstanding? Does anyone have any ideas? I'm planning to use the LabelFunction to format data in the datagrid i.e. turn data from a number code into a descriptive string and this issue is causing me problems.

      Here's a sample of my test code:

      <mx:Script>
      <![CDATA[
      import mx.controls.dataGridClasses.DataGridColumn;

      private function test_labelFunc(item:Object, column:DataGridColumn):String {
      trace(item.@name);
      item.@name+="A";
      return item.@name;
      }
      ]]>
      </mx:Script>

      <mx:XML id="itemsXML">
      <items>
      <item name="Item 1"/>
      <item name="Item 2"/>
      <item name="Item 3"/>
      <item name="Item 4"/>
      </items>
      </mx:XML>

      <mx:DataGrid id="dataGrid" dataProvider="{itemsXML.item}">
      <mx:columns>
      <mx:DataGridColumn dataField="@name"
      headerText="Name:"
      labelFunction="test_labelFunc" />
      </mx:columns>
      </mx:DataGrid>

      and I get output:

      Item 1AA
      Item 2A
      Item 3A
      Item 4A

      where "Item 1AA" should be "Item 1A"

      Thanks,
      Tracy
        • 1. Re: LabelFunction called twice for first row of DataGrid
          Arty13
          Try this...

          private function test_labelFunc(item:Object, column:DataGridColumn):String {
          return item.@name + "A";
          }

          This is only changeing the display values and not the actual values of the XML data.

          If you are intending to change the XML data, I would recomend doing it differently than through the Label Function. Because the label function will get called each time there is change in view (i think) for example, if you sort the column, each value will get another A onto the label.

          Hope this helps you out.

          Art
          • 2. Re: LabelFunction called twice for first row of DataGrid
            ntsiii Level 3
            Yes, changing the item's values in a lableFunction is, ... well, weird. Inadvisable, there is a better word.

            One thing I have found is that sometimes the labelFunction gets called witha null item. I always check:
            if (item) {
            do stuff
            }

            Tracy Spratt
            • 3. Re: LabelFunction called twice for first row of DataGrid
              TAB68 Level 1
              Thanks for your reply. I was only intending to change the way the data is displayed in the datagrid rather than changing the actual data itself. I guess I was just misunderstanding how the labelfunction worked. I've now realised that returning a formatted string is what I actually need to do rather than changing the data. Doh!

              Although it's not affecting me now, I suppose it still begs the question as to why the labelfunction is called twice for the first row in the datagrid. Is it a bug?