What about something along these lines:

private function selectComboItem( cb : ComboBox, item : String, itemField : 
String = null ) : int
{
  var dp : ICollectionView = cb.dataProvider;
  var i : uint = 0;

  if ( itemField == null )
    itemField = cb.labelField;

  for each ( var dpi : Object in dp )
  {
    if ( dpi[ itemField ] == item )
    {
      cb.selectedIndex = i;
      return i;
  }

  return -1;
}

Obviously this needs a lot more work to be 100% generic, since the dpi's can be 
any kind of object, including primitives like String and XML in the case of 
XMLList based data providers, where you will need to use something like (dpi as 
XML).attribute( itemField )[0] to get the required item field for comparison.

--- In flexcoders@yahoogroups.com, "bhaq1972" <mbha...@...> wrote:
>
> sorry, i meant
> var item:String = xmlLC.getItemAt(i)[collectionID];
> 
> --- In flexcoders@yahoogroups.com, "bhaq1972" <mbhaque@> wrote:
> >
> > > Generic form>>
> > > var item:String = xmlLC.getItemAt(i).collectionID; ??
> > > 
> > 
> > Try this
> > 
> > var item:String = xmlLC.getItemAt(i)["collectionID"];
> > 
> > 
> > 
> > 
> > --- In flexcoders@yahoogroups.com, Angelo Anolin <angelo_anolin@> wrote:
> > >
> > > Actually, the dpID is different for each xmllistcollection being passed.
> > > 
> > > For example, for an xmllistcollection consisting of Orders, I might have 
> > > to need the OrderID, which I am accessing, and it goes on for another 
> > > xmllistcollection of States, where on that collection, I might have to 
> > > access StatesID.
> > > 
> > > To give further clarification,
> > > 
> > > say I have two combo boxes, cmbTeams and cmbCoach.  the dataprovider for 
> > > cmbTeams is xmllcTeams and for cmbCoach is xmllcCoach.
> > > 
> > > I want a generic function such that I would be able to set the 
> > > selectedItem for each Combo Box instead of writing two.
> > > 
> > > private function SetComboBoxSelectedItem(itemToSelect:String, 
> > > xmlLC:XMLListCollection, cmb:ComboBox, collectionID:String)
> > > 
> > > Since the collectionID for the xmllcTeams is TeamID, then that is the 
> > > value which I pass as collectionID in the function.  For the xmllcCoach, 
> > > it would be the CoachID.
> > > 
> > > 
> > > so what comes now is that when I want to call the function, the 
> > > collectionID is represented by the actual ID which to match in the data 
> > > provider.
> > > 
> > > For the xmllcTeams
> > > var item:String = xmlLC.getItemAt(i).TeamID
> > > 
> > > For the xmllcCoach
> > > var item:String = xmlLC.getItemAt(i).CoachID
> > > 
> > > 
> > > Generic form>>
> > > var item:String = xmlLC.getItemAt(i).collectionID; ??
> > > 
> > > Thanks.
> > > 
> > > 
> > > 
> > > ________________________________
> > > From: gareth_arch <gareth_arch@>
> > > To: flexcoders@yahoogroups.com
> > > Sent: Wed, 19 May, 2010 10:34:35
> > > Subject: [flexcoders] Re: Generic Function To Set ComboBox Item
> > > 
> > >   
> > > Don't you access properties in an XMLListCollection differently than in 
> > > an ArrayCollection?
> > > 
> > > So rather than...
> > > var item:String = dataProv.getItemAt(i).dpID;
> > > it would be
> > > var item:String = dataProv.getItemAt(i)....@dpid;
> > > 
> > > Try doing a debug of your code and see what is being returned also.
> > > 
> > > --- In flexcoders@yahoogroups.com, Angelo Anolin <angelo_anolin@> wrote:
> > > >
> > > > Hi FlexCoders,
> > > > 
> > > > I would want to create a generic function to be able to set a ComboBox 
> > > > item.
> > > > 
> > > > The function I have goes like this:
> > > > 
> > > > private function selectComboItem(itemID:String, 
> > > > dataProv:XMLListCollection, cmb:ComboBox, dpID:String) :void
> > > > {
> > > >   for (var i:int = 0; i< dataProv.length; i++)
> > > >   {
> > > >     var item:String = dataProv.getItemAt(i).dpID;
> > > > 
> > > >     if(itemID == item)
> > > >     {
> > > >       cmb.selectedIndex = i;
> > > >       break;
> > > >     }
> > > >   }
> > > > }
> > > > 
> > > > dpID is the data item in the XML List collection which should match the 
> > > > passed itemID.  But I can't seem to get properly the item.
> > > > 
> > > > Any suggestions highly appreciated. Thanks.
> > > >
> > >
> >
>


Reply via email to