我認爲您對<apex:actionSupport>
VF標籤感興趣,它允許您在表單元素上執行操作時調用控制器中的操作方法和頁面上的刷新和組件。在這種情況下,您需要將其綁定到您的<apex:selectList>
的onchange
事件。
我最近爲另一個項目組合了一些類似的東西,所以我對它進行了一些改動,以便爲您概括它。下面的頁面/控制器應該允許您查看任何文件夾中的電子郵件模板......您應該可以根據自己的目的修改它,而不會有太多困難。
VF頁:
<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>
控制器:
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;
}
}
來源
2012-08-02 15:20:52
JCD