I think you're interested in the <apex:actionSupport>
VF tag, which allows you to call an action method in the controller and refresh and component on the page when an action is performed on a form element. In this case you would want bind it to the onchange
event of your <apex:selectList>
.
I had recently put together something similar for another project, so I changed it a bit to generalize it for you. The page/controller below should allow you to view email templates in any folder...you should be able to modify it for your purposes without too much difficulty.
VF page:
<apex:page controller="SomeController">
<apex:form>
<apex:pageBlock title="View Email Template" id="emailTemplateBlock">
Select Email Template Folder:
<apex:selectList value="{!selectedEmailTemplateFolder}" multiselect="false" size="1">
<apex:selectOptions value="{!emailTemplateFolderOpts}" />
<apex:actionSupport event="onchange" action="{!refreshEmailTemplateSection}" rerender="emailTemplateBlock" />
</apex:selectList>
<br/>
Select Email Template:
<apex:selectList value="{!selectedEmailTemplate}" multiselect="false" size="1">
<apex:selectOptions value="{!emailTemplateOpts}" />
<apex:actionSupport event="onchange" action="{!refreshEmailTemplateSection}" rerender="emailTemplateBlock" />
</apex:selectList>
<br/>
<apex:outputPanel id="emailTemplateContentPanel" rendered="{!NOT(ISNULL(chosenEmailTemplate))}">
<p><b>{!chosenEmailTemplate.Name} - Subject:</b> {!chosenEmailTemplate.Subject}</p>
<pre>{!chosenEmailTemplate.Body}</pre>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class SomeController {
private List<EmailTemplate> allEmailTemplates;
private List<Folder> allEmailTemplateFolders;
public String selectedEmailTemplateFolder {public get; public set;}
public String selectedEmailTemplate {public get; public set;}
public EmailTemplate chosenEmailTemplate {public get; private set;}
public SomeController() {
allEmailTemplates = [select Id, Name, Subject, Body, FolderId from EmailTemplate order by Name asc];
allEmailTemplateFolders = [select Id, Name from Folder where Type = 'Email' order by Name asc];
}
public List<SelectOption> getEmailTemplateFolderOpts() {
List<SelectOption> opts = new List<SelectOption>();
opts.add(new SelectOption('null', ' - Select - '));
for ( Folder f : allEmailTemplateFolders )
opts.add(new SelectOption(f.Id, f.Name));
// add an option for templates in the Unfiled Public folder
opts.add(new SelectOption(UserInfo.getOrganizationId(), 'Unfiled Public'));
return opts;
}
public List<SelectOption> getEmailTemplateOpts() {
List<SelectOption> opts = new List<SelectOption>();
if ( selectedEmailTemplateFolder != null ) {
opts.add(new SelectOption('null', ' - Select - '));
Id selectedId = (Id) selectedEmailTemplateFolder;
for ( EmailTemplate et : allEmailTemplates ) {
if ( et.FolderId == selectedId )
opts.add(new SelectOption(et.Id, et.Name));
}
}
return opts;
}
public PageReference refreshEmailTemplateSection() {
// this is a bit ugly, but a consequence of not being able to set a SelectOption's value to null
if ( selectedEmailTemplateFolder == null || selectedEmailTemplateFolder == 'null' ) {
selectedEmailTemplateFolder = null;
selectedEmailTemplate = null;
}
if ( selectedEmailTemplate == null || selectedEmailTemplate == 'null' ) {
selectedEmailTemplate = null;
chosenEmailTemplate = null;
} else {
Id selectedId = (Id) selectedEmailTemplate;
for ( EmailTemplate et : allEmailTemplates ) {
if ( et.Id == selectedId ) {
chosenEmailTemplate = et;
break;
}
}
}
return null;
}
}