Dynamic Drop Down Population

Dynamic Drop Down Population

I have found this useful and assume others will as well. For this activity, I have a form that allows the user to select a sport, which is a lookup field to my sports form. Once a sport is selected, I have two drop down fields that are for event types and location. These 2 fields will be populated based on the sport selection, by getting the record of the sport and then checking for the event types and locations registered to that sport. The code is as follows:

  • // create empty collections to hold the fetched data
  • locations = Collection();
  • event_types = Collection();
  • //make sure that a sport value is selected
  • if(Team.Team_Name != null || Team.Team_Name != "")
  • {
  • // fetch the data for the sport
  • sport_data = Sports[Sport_Name == input.Team.Sports.Sport_Name];
  • if(sport_data.count() > 0)
  • {
  • //start filling our event collection variable
  • for each  event in sport_data.Event_Types
  • {
  • event_types.insert(event.Event_Type);
  • }
  • //start filling our event location collection
  • for each  location in sport_data.Locations
  • {
  • locations.insert(location.Location);
  • }
  • }
  • //add all of our collections to their respective drop downs
  • input.Event:ui.add(event_types);
  • input.Location:ui.add(locations);
  • }