1. 程式人生 > >salesforce零基礎學習(七十四)apex:actionRegion以及apex:actionSupport淺談

salesforce零基礎學習(七十四)apex:actionRegion以及apex:actionSupport淺談

xxx turn 組件 聯動 異步 action cti 相關 bottom

我們在開發中,很難會遇見不提交表單的情況。常用的apex:commandButton,apex:commandLink,apex:actionFunction,apex:actionSupport。他們進行操作的時候,會將整個表單提交。但是我們很多時候的需求,只是希望提交一部分內容,而不是全部。這個時候,我們就需要用到apex:actionRegion.

一.apex:actionRegion

apex:actionRegion為當一個ajax請求生成的時候,可以通過它來區分哪部分區域/組件可以應該被Force.com Service處理,只有在actionRegion內部的組件元素被處理。

apex:actionRegion用於apex:form中涉及到表單提交指定區域塊或者組件的內容,如果一個表單提交請求需要提交整個頁面,則不必使用apex:actionRegion。

apex:actionRegion盡管可以用於ajax請求的時候,指定區域提交,但是卻無法定義請求完成後的reRender區域,reRender操作仍然在相關的控件的reRender部分來操作,即apex:actionRegion作用只在於提交表單時可以指定區域內容來提交。

二.apex:actionSupport

actionSupport作用為當一個特定的事件被觸發,比如單擊,失去焦點,鼠標移入等操作被觸發時,允許組件進行異步的刷新功能,常用場景為聯動,失去焦點後校驗在數據庫中是否唯一等。

actionSupport和actionFunction在功能上很相像,不過有幾點小區別:

1.actionSupport是直接被其他的元素調用,actionFunction可以通過js調用,你可以在js中進行一些簡單的處理,也可以直接被其他元素調用;

2.actionSupport僅允許在單一的事件上調用action的method,actionFunction可以被多個事件調用。

demo舉例:

1.TestActionSupportController:設置一個testOptions變量,初始值是xxx,當調用changeOptions後變成yyy

 1 public with sharing class TestActionSupportController {
 2     
 3     public Account acc{get;set;}
 4 
 5     public String testListValue{get;set;}
6 7 public List<SelectOption> testOptions { 8 get { 9 if(testOptions == null) { 10 testOptions = new List<SelectOption>(); 11 testOptions.add(new SelectOption(‘xxx‘,‘xxx‘)); 12 } 13 return testOptions; 14 }set; 15 } 16 17 public void changeOptions() { 18 testOptions = new List<SelectOption>(); 19 testOptions.add(new SelectOption(‘yyy‘,‘yyy‘)); 20 } 21 }

2.TestActionSupport.page:顯示一個必填輸入框以及一個下拉列表,下拉列表控制著另外一個下拉列表的顯示。

 1 <apex:page controller="TestActionSupportController">
 2     <apex:form id="theForm">
 3         <apex:inputField value="{!acc.Name}" required="true" style="padding-bottom: 5px;"/>
 4         <br/>
 5         <apex:outputPanel id="industryPanel" style="padding-bottom: 5px;">
 6             <apex:inputField value="{!acc.Industry}"/>
 7             <apex:actionSupport event="onchange" action="{!changeOptions}" reRender="testSelectList"/>
 8         </apex:outputPanel>
 9         <apex:selectList value="{!testListValue}" id="testSelectList" style="margin-left: 5px;">
10             <apex:selectOptions value="{!testOptions}">
11             </apex:selectOptions>
12         </apex:selectList>
13     </apex:form>
14 </apex:page>

顯示效果如下:

技術分享

其實上面的vf代碼是有問題的,當選擇了下拉框,右側的下拉框的值也不修改成yyy,原因是這樣的:

當actionSupport執行時,會提交整個form表單,因為上面有一個required的字段,所以導致提交表單失敗,沒有走action對應的後臺的的method,所以沒有更新下拉框裏面的值,這種情況有兩種可以修改的方式,1是將actionSuuport設置成immediate="true"方式,可以忽略表單的validation,第二種是使用actionRegion方式,這樣from表單提交時只提交actionRegion內的數據,從而避免了上面的尷尬,使用actionRegion方式修改如下:

 1 <apex:page controller="TestActionSupportController">
 2     <apex:form id="theForm">
 3         <apex:inputField value="{!acc.Name}" required="true" style="padding-bottom: 5px;"/>
 4         <br/>
 5         <apex:actionRegion>
 6             <apex:outputPanel id="industryPanel" style="padding-bottom: 5px;">
 7                 <apex:inputField value="{!acc.Industry}"/>
 8                 <apex:actionSupport event="onchange" action="{!changeOptions}" reRender="testSelectList"/>
 9             </apex:outputPanel>
10             <apex:selectList value="{!testListValue}" id="testSelectList" style="margin-left: 5px;">
11                 <apex:selectOptions value="{!testOptions}">
12                 </apex:selectOptions>
13             </apex:selectList>
14         </apex:actionRegion>
15     </apex:form>
16 </apex:page>

顯示效果:當更改下拉框的之,會修改成yyy

技術分享

總結:使用actionRegion和actionSupport可以更加靈活的提交表單,提升表單提交的性能。內容描述錯誤的地方歡迎指出,有問題歡迎留言。

salesforce零基礎學習(七十四)apex:actionRegion以及apex:actionSupport淺談