动态添加行并在刷新时维护值

时间:2013-08-05 22:25:02

标签: salesforce apex-code visualforce

我有一个VF页面,当我点击“添加参与者”按钮时,我可以动态地向pageBlockTable添加行。我在页面上工作,我可以添加行,但是当我添加新行时,我正在丢失行中的值。值被清除。

在添加新行时,有人可以帮助我如何维护我的值吗?我正在使用包装类。

这是我的页面:

<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;
    }
}
}

在点击保存按钮之前,不会保存数据,我还没有实现。我只是想在添加新行时让值保持其状态。 refreshLineItems方法清除了值,但是当记录已经存在时我需要该方法。所以,我的问题是如何维护尚未保存到数据库的行中的值?我试图在我的包装类中处理它,但没有成功。

非常感谢任何帮助! 感谢。

1 个答案:

答案 0 :(得分:1)

嗨,你失去了值,因为在apex中的immediate =“true”标签:commandButton。 您可以在inputField中使用和“required = false”。