Salesforce 在刷新时动态添加行并维护值
我有一个VF页面,当单击“添加参与者”按钮时,我可以动态地将行添加到pageBlockTable中。我有一个可以添加行的页面,但是当我添加新行时,我丢失了行中的值。这些值被清除 有人能帮助我在添加新行时如何维护我的值吗?我正在使用包装器类 这是我的页面:Salesforce 在刷新时动态添加行并维护值,salesforce,apex-code,visualforce,Salesforce,Apex Code,Visualforce,我有一个VF页面,当单击“添加参与者”按钮时,我可以动态地将行添加到pageBlockTable中。我有一个可以添加行的页面,但是当我添加新行时,我丢失了行中的值。这些值被清除 有人能帮助我在添加新行时如何维护我的值吗?我正在使用包装器类 这是我的页面: <apex:page standardController="Call_Report__c" extensions="CallReportControllerExtension" showHeader="true" sidebar="tr
<apex:page standardController="Call_Report__c" extensions="CallReportControllerExtension" showHeader="true" sidebar="true">
<apex:sectionHeader title="Call Report Edit" subtitle="{!IF(isEditMode, 'New Call Report', CallReportName)}" />
<apex:includeScript value="{!URLFOR($Resource.JQueryUI, '/js/jquery-1.8.2.min.js')}" />
<apex:includeScript value="{!URLFOR($Resource.JQueryUI, '/js/jquery-ui-1.9.0.custom.js')}" />
<script type="text/javascript">
var j$ = jQuery.noConflict();
j$(document).ready(function(){
j$('.errorMsg').hide();
});
</script>
<script>
var newWin=null;
function openLookupPopup(name, id)
{
var url="/apex/ParticipantSearchPopup?namefield=" + name + "&idfield=" + id;
newWin=window.open(url, 'Popup','height=350,width=400,left=100,top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
if (window.focus)
{
newWin.focus();
}
return false;
}
function closeLookupPopup()
{
if (null!=newWin)
{
newWin.close();
}
}
</script>
<apex:form >
<apex:pageMessages id="Errors" />
<apex:pageBlock title="Call Report Edit" mode="edit">
<apex:pageBlockButtons location="both">
<apex:commandButton action="{!save}" value="Save" id="theSaveButton" />
<apex:commandButton action="{!cancel}" value="Cancel" id="theCancelButton" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Information" columns="2">
<apex:inputField value="{!Call_Report__c.Name}" required="true" />
<apex:pageBlockSectionItem rendered="{!!isEditMode}" >
<apex:outputLabel value="Owner" />
<apex:outputField value="{!Call_Report__c.Owner.Name}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!isEditMode}" >
<apex:outputLabel value="Owner" />
<apex:outputLabel value="{!$User.FirstName} {!$User.LastName}" />
</apex:pageBlockSectionItem>
<apex:inputField value="{!Call_Report__c.Location__c}" />
<apex:inputField value="{!Call_Report__c.EventAmount__c}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Detail" columns="1">
<apex:inputTextArea value="{!Call_Report__c.Purpose__c}" cols="75" />
<apex:inputTextArea value="{!Call_Report__c.Results__c}" cols="75" />
<apex:inputTextArea value="{!Call_Report__c.Next_Steps__c}" cols="75" />
<apex:inputField value="{!Call_Report__c.Description__c}" />
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock >
<apex:pageBlockButtons location="top" id="topButton">
<apex:commandButton id="newButton" value="Add Participant" action="{!addParticipant}" rerender="pageTable" immediate="true" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Participants" columns="1">
<apex:pageBlockTable id="pageTable" value="{!participantLinesForPage}" var="part">
<apex:column headerValue="Account" width="25%">
<apex:inputHidden value="{!part.participantLine.Account__r.Id}" id="targetAccountId" />
<apex:inputText value="{!part.participantLine.Account__r.Name}" id="targetAccountName" onFocus="this.blur()" disabled="false" style="width:175px;" />
<a href="#" onclick="openLookupPopup('{!$Component.targetAccountName}', '{!$Component.targetAccountId}'); return false" ><img onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onblur="this.className = 'lookupIcon';" class="lookupIcon" src="/s.gif" /></a>
</apex:column>
<apex:column headerValue="Contact" width="25%">
<apex:inputHidden value="{!part.participantLine.Contact__r.Id}" id="targetContactId" />
<apex:inputText value="{!part.participantLine.Contact__r.Name}" id="targetContactName" onFocus="this.blur()" disabled="false" style="width:175px;" />
<a href="#" onclick="openLookupPopup('{!$Component.targetContactName}', '{!$Component.targetContactId}'); return false" ><img onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onblur="this.className = 'lookupIcon';" class="lookupIcon" src="/s.gif" /></a>
</apex:column>
<apex:column headerValue="User" width="25%">
<apex:inputHidden value="{!part.participantLine.User__r.Id}" id="targetUserId" />
<apex:inputText value="{!part.participantLine.User__r.Name}" id="targetUserName" onFocus="this.blur()" disabled="false" style="width:175px;" />
<a href="#" onclick="openLookupPopup('{!$Component.targetUserName}', '{!$Component.targetUserId}'); return false" ><img onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onblur="this.className = 'lookupIcon';" class="lookupIcon" src="/s.gif" /></a>
</apex:column>
<apex:column headerValue="Spent Amount" width="25%">
<apex:inputField value="{!part.participantLine.Spent_Amount__c}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
这是我的控制器:
public with sharing class CallReportControllerExtension {
private final Call_Report__c callReport;
public Boolean isEditMode {get; set;}
public List<Participants> participantLinesForPage {get; set;}
public CallReportControllerExtension(ApexPages.StandardController stdController) {
this.callReport = (Call_Report__c)stdController.getRecord();
isEditMode = isEditPage(ApexPages.currentPage().getParameters().get('save_new'));
refreshLineItems();
}
public String getCallReportName() {
return [select Name from Call_Report__c where Id =: callReport.Id].Name;
}
public PageReference addParticipant() {
Participant__c newRecord = new Participant__c();
//newRecord.Account__c = '001f0000007qcD5';
//newRecord.Contact__c = '003f0000007H61z';
//newRecord.User__c = '005f0000000U5ME';
//newRecord.Spent_Amount__c = 100.00;
participantLinesForPage.add(new Participants(participantLinesForPage.size(), newRecord));
return null;
}
private void refreshLineItems() {
List<Participant__c> lineItems = [select Account__c, Account__r.Id, Account__r.Name, Contact__c, Contact__r.Id, Contact__r.Name, User__c, User__r.Id, User__r.Name, Spent_Amount__c from Participant__c where Call_Report__c =: callReport.Id];
participantLinesForPage = new List<Participants>();
Integer iterate = 0;
for(Participant__c p : lineItems) {
participantLinesForPage.add(new Participants(iterate, p));
iterate += 1;
}
}
private Boolean isEditPage(String param) {
Boolean retval = false;
if(param != null) {
retval = true;
}
return retval;
}
class Participants {
public Integer iterate {get; set;}
public Participant__c participantLine {get; set;}
public Participants(Integer iterate, Participant__c participantLine) {
this.iterate = iterate;
this.participantLine = participantLine;
}
}
}
public与共享类CallReportControllerExtension{
私人最终通话报告;
公共布尔isEditMode{get;set;}
公共列表参与者行页面{get;set;}
public CallReportControllerExtension(ApexPages.StandardController stdController){
this.callReport=(Call_Report_uuc)stdController.getRecord();
isEditMode=isEditPage(ApexPages.currentPage().getParameters().get('save_new');
refreshLineItems();
}
公共字符串getCallReportName(){
return[从Call\u Report\u c中选择名称,其中Id=:callReport.Id].Name;
}
公共页面引用addParticipant(){
参与者__cnewrecord=新参与者__c();
//newRecord.Account_uuc='001f0000007qcD5';
//newRecord.Contact_uuc='003f0000007H61z';
//newRecord.User_uuc='005f0000000U5ME';
//newRecord.spended_Amount__c=100.00;
添加(新参与者(participantLinesForPage.size(),newRecord));
返回null;
}
私有无效refreshLineItems(){
列表行项目=[选择帐户、帐户Id、帐户名称、联系人c、联系人Id、联系人姓名、用户c、用户Id、用户Id、用户名称、用户名称、用户名称、用户名称、参与者c的花费金额(其中Call_Report_c=:callReport.Id];
participantLinesForPage=新列表();
整数迭代=0;
对于(参与者p:行项目){
添加(新参与者(迭代,p));
迭代+=1;
}
}
私有布尔isEditPage(字符串参数){
布尔retval=false;
如果(参数!=null){
retval=true;
}
返回返回;
}
班级参与者{
公共整数迭代{get;set;}
公共参与者\uuuu c participantLine{get;set;}
公共参与者(整数迭代,参与者\ c参与者行){
this.iterate=迭代;
this.participantLine=participantLine;
}
}
}
在单击save按钮之前,数据不会被保存,我还没有实现它。我只是想让这些值在添加新行时保持它们的状态。refreshLineItems方法正在清除值,但当记录已经存在时,我需要该方法。所以,我的问题是如何维护尚未保存到数据库的行中的值?我试图在包装器类中处理它,但没有成功
非常感谢您的帮助!
谢谢。您好,您正在丢失值,因为apex:commandButton中有immediate=“true”标记。
您可以改为在inputField中使用和“required=false”。您好,您会因为apex:commandButton中的immediate=“true”标记而丢失值。
您可以在inputField中使用和“required=false”。您好,谢谢您的回复。我尝试从命令按钮中删除了
immediate=“true”
,也从inputField中删除了required=“false”
属性。值仍然被清除。有趣的是,我有一个字段不是查找,它是pageBlockTable,并且它的值没有被清除。只有查找字段的值才会被清除?我不知道为什么viewstate没有保持这些值?您好,谢谢您的回复。我尝试从命令按钮中删除了immediate=“true”
,也从inputField中删除了required=“false”
属性。值仍然被清除。有趣的是,我有一个字段不是查找,它是pageBlockTable,并且它的值没有被清除。只有查找字段的值才会被清除?我不确定viewstate为什么不维护这些值?