有趣的是,它始终保留您的 DataSet 的相同副本,该副本每 5 秒刷新一次并显示,这意味着您的 ViewState 也每 5 秒刷新一次,在我看来,您可能会提交上次收到的相同值,但不包含任何改变,我会走两条路线之一:
- 在编辑模式或进行更改时停止轮询器也阻止编辑记录,这样其他人就无法覆盖您的更改。
- 将 viewState 拆分为两个单独的 ViewState,一个用于加载记录,第二个用于编辑选定的记录,当保存第一个 ViewState 时,将使用对第二个 ViewState 所做的最后更改进行刷新,您可以通过使用来完成此操作多种形式和同步机制。
使用选项(1)的建议解决方案,轮询器保持活动状态,不会干扰记录,选择时仍然缺少记录的锁定机制
视觉力量页面
<apex:page controller="MultipleActionRegionsCtrl">
<!-- SINGLE FORM -->
<apex:form >
<apex:pageBlock title="Master Detail Record Selection/Edition">
<!-- MASTER LIST -->
<apex:pageBlockSection title="Available Records">
<apex:actionRegion >
<!-- TABLE-->
<apex:outputPanel id="recordsPanel">
<apex:PageBlockTable value="{!cList}" var="c">
<apex:column value="{!c.FirstName}"/>
<apex:column value="{!c.LastName}"/>
<apex:actionSupport event="onRowClick" action="{!editSelectedRecord}" rerender="recDetail" status="dataUpdateStatus">
<apex:param name="cid" value="{!c.Id}" />
</apex:actionSupport>
</apex:PageBlockTable>
</apex:outputPanel>
<apex:actionStatus id="dataRefreshStatus">
<apex:facet name="start">Refreshing...</apex:facet>
<apex:facet name="stop">Data Loaded</apex:facet>
</apex:actionStatus>
<!-- RELOAD TABLE-->
<apex:actionPoller action="{!reloadContacts}" reRender="recordsPanel" interval="5" status="dataRefreshStatus"/>
</apex:actionRegion>
</apex:pageBlockSection>
<!-- DETAIL -->
<apex:pageBlockSection title="Record Details" id="recDetail" columns="2">
<apex:actionRegion rendered="{!IF(editableRecord=null,false,true)}">
<table>
<tr>
<td colSpan="2">
<apex:actionStatus id="dataUpdateStatus" >
<apex:facet name="start">Loading...</apex:facet>
<apex:facet name="stop"> </apex:facet>
</apex:actionStatus>
</td>
</tr>
<tr>
<td>First Name </td>
<td><apex:inputField value="{!editableRecord.FirstName}"/></td>
</tr>
<tr>
<td>Last Name </td>
<td><apex:inputField value="{!editableRecord.LastName}"/></td>
</tr>
<tr>
<td><apex:commandButton action="{!saveRecord}" value="Save" reRender="recordsPanel,recDetail"/></td>
</tr>
</table>
</apex:actionRegion>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
控制器
public class MultipleActionRegionsCtrl {
public Map<ID, Contact> cMap {get;set;}
public Contact editableRecord {get;set;}
// Using lazy load for test purposes
public List<Contact> cList {
get{
if(cMap == null){
cMap = new Map<ID, Contact>([SELECT Id, FirstName, LastName From Contact limit 10]);
}
return cMap.values();
}
}
public MultipleActionRegionsCtrl(){ }
public PageReference reloadContacts(){
if(cMap!=null && !cMap.isEmpty()){
Set<Id> myIds = cMap.keySet();
// reload same records loaded at the start
cMap = new Map<ID, Contact>([SELECT Id, FirstName, LastName From Contact WHERE Id IN :myIds]);
}
return null;
}
public PageReference editSelectedRecord() {
String cID = ApexPages.currentPage().getParameters().get('cid');
if(cMap!=null && !cMap.isEmpty() && cMap.containsKey(cID)){
editableRecord = cMap.get(cID);
}
return null;
}
public PageReference saveRecord(){
update editableRecord;
editableRecord = null; // so we don't save two times same record
return reloadContacts(); //instantly update current list do not wait for poller
}
}