2 Replies Latest reply on Dec 7, 2007 4:28 AM by CFuffle

    labelFunction throwing error

    CFuffle
      Hi,

      I have a combobox which I am populating from a remote XML file. The dropdown list needs to contain an owner name and surname concatenated with a space. I found out how to do this using the labelFunction however things don't seem to be working. The structure of my XML is as follows:

      <owners>
      <owner>
      <owner_fname>First name</owner_fname>
      <owner_sname>Surname</owner_sname>
      </owner>
      </owners>

      - The results of the http request are put into an arrayCollection called ownerList

      My combobox looks like this:
      <mx:ComboBox id="ownerCombo" dataProvider="{ownerList}" labelFunction="myLabelFunc" rowCount="10"></mx:ComboBox>

      My function looks like this (for testing purposes I'm just trying to return a surname for the moment):
      public function myLabelFunc(item:Object):String {
      return ownerList.owners.owner.owner_sname;
      }

      I am getting the following error:
      Access of possibly undefined property owners through a reference with static type mx.collections:ArrayCollection.

      I have tried every variation of the owners dot notation but everything seems to give a similar error to above.

      Note:
      - The combobox works when I directly use the labelField property so the whole arrayCollection seems to be working.

      Any help greatly appreciated as I am stumped on this one,

      cheers,

      Mike
        • 1. Re: labelFunction throwing error
          peterent Level 2
          I'm assuming that when you use the dataProvider of {ownerList} you've set ownerList to be an XMLList. That being the case, then the "item" parameter to your labelFunction will be one of the <owner> nodes. So the surname would be:

          return XML(item).owner_sname;

          • 2. labelFunction throwing error
            CFuffle Level 1
            Thanks for the reply Peter. It was a lack of understanding of ActionScript on my part. My function now looks like the below. The null is to allow for the fact that sometimes only first names are in the db table and dataGrid labels show up as 'null', so if there's null value I only return the first name (if only we all had clean db's, sigh!).

            public function myLabelFunc(item:Object):String {
            var labelString:String;
            if (item.owner_sname==null){
            labelString=item.owner_fname;
            }else{
            labelString=item.owner_sname + " " + item.owner_fname;
            }
            return labelString;
            }

            ...which works perfectly. I didn't realise that 'item' was being used to represent the current item in the arrayCollection. Once I got it into my head that 'item' represents the current row of data I then just tagged the particular field I was looking for onto the end (item.owner_fname),

            hope this helps someone in similar trouble,

            many thanks,

            Mike