function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ekorzekorz 

SOQL built Picklist that drives the rest of the page

I'm pretty new to VF, so I'm erring on the side of verbosity.  I'm also a soccer fan.  I hope that helps?

I'm trying to build a nice way for a user to 'browse' their important records.  Basically, I'm hoping to have a filtered picklist at the top of the page, and then a table of fields/values for whatever picklist value was selected.  Imagine my users are Coaches of different soccer teams, and they want to browse their player's stats.  I could build this with a lookup field driving everything, but that seems weird for my use case.

So imagine my database is full of all soccer_players_c from teams all over the world.   This VF page would be for a coach, and they really don't care about other team's players, so I'd reduce the driving picklist to just their team's players. 

Custom object Soccer_Player__c
Field values Goals__c, Age__c, Salary__c, maybe a nice photo?

So I'd want to do some SOQL in a controller building my picklist:  [select Name from Soccer_Player__c where Team__c =$user.team]
Then display that picklist at the top of the page.  Then, whenever you click on a new player 'Name', the rest of the page updates (ajax I guess?) showing Goals, Age, Salary, maybe that nice photo... etc. 

If this is totally covered in a cookbook somewhere I'm happy to read up.  I don't think I had the right keywords when I was searching.  Otherwise if you have an example controller / page that covers this I'd love to see it!
Best Answer chosen by ekorz
ekorzekorz
OK well that didn't quite do it, but I did solve the problem. 

If anyone stumbles though this thread, I ended up driving the Related List portion of the VF page, showing the related "players" and stats, through a nice component:  https://github.com/cloudspokes/generic-related-list

The SOQL driven list part, showing available "teams" to pick from, I'll paste here though.  This page has a drop-down list generated by the user's ID, assuming that the field "Coach__c" on the Team__c object is a user reference.  The list would populate with the Names of the teams the user 'coached', and after it's selected, the page updates with id?=[that id].  The component above grabs that id and show the related 'players'


=-=--=-=VF Page "Page1"==-=-=--=-=-=-=


<apex:page controller="SelectTest" showheader="false" sidebar="false">
      <apex:form >
          <apex:pageBlock >
              <apex:pageBlockSection columns="3">
                  <apex:selectList value="{!selected}" size="1" styleClass="greyButton">                     
                      <apex:selectOptions value="{!teams}"/>                     
                      <apex:actionSupport event="onchange" action="{!RedirectToTeam}" />

                   // the generic-related-list component code goes here, displaying a list of 'players' & stats based on the apex/page?id= value

                  </apex:selectList>                          
              </apex:pageBlockSection>
          </apex:pageBlock>
      </apex:form>
  </apex:page>



-=--=-==-=-CONTROLLER-==--=-=-=-=-=-=

public with sharing class SelectTest{
 
    public String selected {get;set;}
    public List<Team__c> TeamList { get; set; }
    public List<SelectOption> teams {get; private set;}
  
    public SelectTest(){
       teams = new List<SelectOption>();
     
       //the SOQL!
       TeamList = [Select ID, Name from Team__c WHERE Coach__c = :UserInfo.getUserId()];
     
       for(integer i=0;i<TeamList.size();i++)
         {  
         teams.add(new SelectOption(TeamList[i].ID,TeamList[i].Name));
         }
   
    }
    }

    public PageReference RedirectToTeam(){
        PageReference pageRef = Page.Page1;
        pageRef.setRedirect(true);
        pageRef.getParameters().put('Id',selected);
        return pageRef;
    }
}

All Answers

Avidev9Avidev9
Well the picklist vaues can be from any db object and even some random string. You can have a look how on http://www.forcetree.com/2009/06/dynamically-add-values-to-picklist.html to use a DB object to populate the picklist values
ekorzekorz
OK well that didn't quite do it, but I did solve the problem. 

If anyone stumbles though this thread, I ended up driving the Related List portion of the VF page, showing the related "players" and stats, through a nice component:  https://github.com/cloudspokes/generic-related-list

The SOQL driven list part, showing available "teams" to pick from, I'll paste here though.  This page has a drop-down list generated by the user's ID, assuming that the field "Coach__c" on the Team__c object is a user reference.  The list would populate with the Names of the teams the user 'coached', and after it's selected, the page updates with id?=[that id].  The component above grabs that id and show the related 'players'


=-=--=-=VF Page "Page1"==-=-=--=-=-=-=


<apex:page controller="SelectTest" showheader="false" sidebar="false">
      <apex:form >
          <apex:pageBlock >
              <apex:pageBlockSection columns="3">
                  <apex:selectList value="{!selected}" size="1" styleClass="greyButton">                     
                      <apex:selectOptions value="{!teams}"/>                     
                      <apex:actionSupport event="onchange" action="{!RedirectToTeam}" />

                   // the generic-related-list component code goes here, displaying a list of 'players' & stats based on the apex/page?id= value

                  </apex:selectList>                          
              </apex:pageBlockSection>
          </apex:pageBlock>
      </apex:form>
  </apex:page>



-=--=-==-=-CONTROLLER-==--=-=-=-=-=-=

public with sharing class SelectTest{
 
    public String selected {get;set;}
    public List<Team__c> TeamList { get; set; }
    public List<SelectOption> teams {get; private set;}
  
    public SelectTest(){
       teams = new List<SelectOption>();
     
       //the SOQL!
       TeamList = [Select ID, Name from Team__c WHERE Coach__c = :UserInfo.getUserId()];
     
       for(integer i=0;i<TeamList.size();i++)
         {  
         teams.add(new SelectOption(TeamList[i].ID,TeamList[i].Name));
         }
   
    }
    }

    public PageReference RedirectToTeam(){
        PageReference pageRef = Page.Page1;
        pageRef.setRedirect(true);
        pageRef.getParameters().put('Id',selected);
        return pageRef;
    }
}
This was selected as the best answer