Salesforce(Visualforce):重新渲染 - 从页面中提取值,而不是记录?

时间:2012-01-23 17:46:16

标签: salesforce visualforce rerender

我在这里坚持我的渲染论点 - 我非常积极地从记录本身而不是从visualforce页面中提取值。当我将值(are_you_kp__c)从记录更改为“否”时 - 页面块部分应该呈现,但由于某种原因它不会。有人知道为什么吗?

我想我需要一些控制器工作 - 但不知道从哪里开始......

<apex:pageBlock title="New Bid" mode="maindetail" tabStyle="Proposal__c">
    <apex:messages/>
    <apex:actionRegion>
    <apex:pageBlockSection columns="2" title="Bid Information" collapsible="false" showHeader="true">
        <apex:inputField value="{!rfp.Bid_Amount__c}"/>
        <apex:outputField value="{!rfp.Bid_Date__c}"/>
        <apex:inputField value="{!rfp.Bid_Deliver_By__c}"/>
        <apex:inputField value="{!rfp.Bid_Comments__c}"/>
        <apex:pageBlockSectionItem>
               <apex:outputLabel value="Are you the Key Appraiser?"/>
               <apex:outputPanel>             
                   <apex:inputField value="{!rfp.are_you_kp__c}" required="true">
                       <apex:actionSupport status="StatusChange" event="onchange" rerender="PageErrors, appraiserInfo"/>
                       <apex:actionStatus startText="Updating page ..." id="StatusChange"/>
                   </apex:inputField>  
               </apex:outputPanel>
        </apex:pageBlockSectionItem> 
    </apex:pageBlockSection>
    </apex:actionRegion>
    <apex:pageBlockSection title="Testing" columns="2" rendered="{!rfp.are_you_kp__c == 'No'}" id="appraiserInfo">
        <apex:pageBlockSectionItem>
            <apex:outputLabel value="Single Point of Contact" for="spoc"/>
                <apex:selectList id="spoc" value="{!rfp.SPOCL__c}" size="1" title="Single Point of Contact Select">
                    <apex:selectOptions value="{!SPOCS}"></apex:selectOptions>
                </apex:selectList>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
</apex:pageBlock>

更新 - 使用正确的id在outputPanel中包装要呈现的元素:仍然存在由于inputField上的更改而切换我呈现的布尔值的问题 - 如何在控制器中切换它?我想我需要评估inputField值是否为No,并将render设置为true - 我不确定如何......

<apex:outputPanel id="appraiserInfo">
<apex:pageBlockSection title="Testing" columns="2" rendered="{!rfp.are_you_kp__c == 'No'}">
    <apex:pageBlockSectionItem>
        <apex:outputLabel value="Single Point of Contact" for="spoc"/>
            <apex:selectList id="spoc" value="{!rfp.SPOCL__c}" size="1" title="Single Point of Contact Select">
            <apex:selectOptions value="{!SPOCS}"></apex:selectOptions>
            </apex:selectList>
    </apex:pageBlockSectionItem>
</apex:pageBlockSection></apex:outputPanel>

还有一次尝试 - 这次它有效,但我不明白为什么......只是它确实如此。除了将action =“{!updateAnswer}”添加到上面的actionSupport(或以下,无论你看哪种方式)

    public pageReference updateAnswer(){
       if(this.p.are_you_kp__c == 'No')
       rfp.are_you_kp__c = 'No';
       try{
        update rfp;
          }
        catch (DmlException ex){
            ApexPages.addMessages(ex);
            return null;
       }
       return null;
    }

可能相关的控制器代码

public ProposalExtension(ApexPages.StandardController pCon) {
    this.p = (Proposal__c)pCon.getRecord();
}

1 个答案:

答案 0 :(得分:5)

将元素包裹在<apex:outputPanel>中并重新渲染,而不是要显示的元素。问题是元素在未渲染时不在页面中,因此它不是重新渲染的工作目标。

这是经常吸引人们的东西,包括我自己 - 我在这里写了一篇关于它的博客文章:http://www.laceysnr.com/2011/10/using-rerender-to-render-one-solution.html

**编辑**

之前没有发现您没有在<apex:actionSupport>标记中指定的操作。您可以使用它来调用控制器上的操作。由于选择列表正在写入您想要的值,因此您实际上不需要在代码中执行任何操作(除非您愿意),您可以这样做:

// controller
public Pagereference UpdateAnswer()
{
  // do some stuff if you want
  return null
}

// page
<apex:actionSupport action="{!UpdateAnswer}" status="StatusChange" event="onchange" rerender="PageErrors, appraiserInfo"/>

希望有所帮助!