Visualforce是一個Web開發框架,容許開發人員構建能夠在Lightning平臺上本地託管的自定義用戶界面。其框架包含:前端的界面設計,使用的相似於HTML的標記語言;以及後端的控制器,使用相似於Java的Apex語言。前端
衆所周知,Salesforce分爲多個版本,不一樣的版本功能之間存在必定的差別,而支持Visualforce的版本:Contact Manager,Group,Professional,Enterprise,Unlimited,Performance和Developer Edition。數據庫
做爲Markup語言,Visualforce有以下優勢:後端
Visualforce頁面有兩個主要元素組成:Markup和Controllerapp
通常說來,不涉及sObject的數據處理(增刪改查)時,僅Markup部分便已足夠;若涉及sObject的數據處理,則需建立一個控制器(class類),並將該類與View綁定。框架
在這裏建立一個下拉菜單選擇並動態刷新的案例。性能
<!--對於單Visualforce Page,全部的Page都必須包含在一個Page內--> <apex:page controller="SP_FilterConditionPageController"> <!--from:Visualforce頁面的一部分,容許用戶輸入和提交按鈕的表單--> <apex:form > <!--outputlabel:輸入輸出字段的標籤--> <apex:outputlabel value="Site Name" for="siteValue" /> <!--selectList:選項列表,容許用戶選擇一個值或多個值--> <apex:selectList value="{!siteName}" size="1" id="siteValue" multiselect="false"> <!--selectOptions:做爲selectList的子組件,提供選擇對象的集合--> <apex:selectOptions value="{!siteItems}"/> </apex:selectList> <apex:outputlabel value="Display Months" for="values2" /> <apex:selectList value="{!displayMonth}" size="1" id="values2" multiselect="false"> <apex:selectOptions value="{!monthItems}"/> </apex:selectList> <!--commandButton:輸入元素的按鈕,按鈕執行有控制器定義,收到響應後刷新頁面--> <apex:commandButton value="Apply" action="{!apply}" rerender="out" status="status"/> </apex:form> <!--outputPanel:將組件組合在一塊兒進行AJAX刷新--> <apex:outputPanel id="out"> <!--顯示AJAX更新請求狀態的組件--> <apex:actionstatus id="status"> <apex:facet name="stop"> </apex:facet> </apex:actionstatus> </apex:outputPanel> </apex:page>
{!**}: 表示controller中的變量。this
服務端的控制器類,爲前端界面的下拉菜單提供數據,並在選擇數據後修改對象對應的字段值。spa
注意:按鈕Action的響應必須爲PageReference.設計
public with sharing class SP_FilterConditionPageController { String displayMonth; String siteName; String currentDisplayMonth; //獲取當前頁面的對象ID Id accountId = ApexPages.CurrentPage().getparameters().get('id'); public String getDisplayMonth() { if(displayMonth == null) { List<Account__c> accounts = [select Display_Months__c, Display_Site__c from Account__c where id = :accountId limit 1]; displayMonth = accounts[0].Display_Months__c; } return displayMonth; } public void setDisplayMonth(String displayMonth) { this.displayMonth = displayMonth; } public String getSiteName() { return siteName; } public void setSiteName(String siteName) { this.siteName = siteName; } public PageReference apply() { if(displayMonth != null || siteName != null){ List<Account__c> accounts = [select Display_Months__c, Display_Site__c from Account__c where id = :accountId limit 1]; for(Account__c account: accounts) { if(displayMonth != null) { account.Display_Months__c = displayMonth; } if(siteName != null) { account.Display_Site__c = siteName; } } if(Schema.sObjectType.Account__c.isUpdateable()) { //更新對象 update accounts; } } //根據當前對象ID,產生新的頁面 PageReference pageRef = new pageReference('/' + accountId); pageRef.setRedirect(true); return pageRef; } public List<SelectOption> getSiteItems() { List<SelectOption> options = new List<SelectOption>(); List<Sites__c> sites = [select WebEx_URL__c, Site_Status__c, Lockdown_Flag__c from Sites__c where Account__c = :accountId]; for(Sites__c site: sites){ if(site.Site_Status__c == 'inactive' || site.Lockdown_Flag__c == 'Not Available') { continue; } options.add(new SelectOption(site.Webex_URL__c, site.Webex_URL__c)); } return options; } public List<SelectOption> getMonthItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('Last 6 Months', 'Last 6 Months')); options.add(new SelectOption('Last 12 Months', 'Last 12 Months')); options.add(new SelectOption('Last 18 Months', 'Last 18 Months')); options.add(new SelectOption('Last 24 Months', 'Last 24 Months')); options.add(new SelectOption('Last 36 Months', 'Last 36 Months')); return options; } }
在上述案例中,前端界面在下拉框中選擇對應的site Name和 Display Name值,點擊Apply按鈕時,將結果保存至數據庫,生成新的頁面返回,這樣即可達到動態刷新頁面的效果。3d
具體的前端界面以下: