VF中的Rerender为什么有时候不好用?

在学习501的课程中,发现如下一段代码

<apex:page controller="TestRerenderExtension">
    <apex:form id="theForm">
        <apex:pageBlock mode="edit" title="Review Edit" id="reviewBlock">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Recommend for Alternate Position?"/>
                    <apex:inputCheckbox value="{!showAltPosition}">
                        <apex:actionSupport event="onchange" rerender="theForm" status="showAltStatus" />
                    </apex:inputCheckbox>      
                </apex:pageBlockSectionItem>       
                <apex:actionStatus startText="(rendering position information...)" id="showAltStatus"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection rendered="{!showAltPosition}" id="altPositionBlock" title="Select the Alternate Position"  columns="1">
             <h1>This is a block</h1>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

这里比较奇怪,实际上要刷新的是“altPositionBlock”,但是为什么不直接用“altPositionBlock”,而要用form呢?

自己把代码改了一下,确实是不好用。


一开始以为是checkbox的值没有穿到后台,但是实际发现确实是传了。

在网上查了一下,发现这种情况需要在需要rerender的Section外层要再包一层outputPanel,然后把rerender制定为对应的outputPanel,而且layout要设置成block。试过后果然好用。(不设置block,HTML中是span,设置成block,HTML中是Div)

<apex:page controller="TestRerenderExtension">
    <apex:form id="theForm">
        <apex:pageBlock mode="edit" title="Review Edit" id="reviewBlock">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Recommend for Alternate Position?"/>
                    <apex:inputCheckbox value="{!showAltPosition}">
                        <apex:actionSupport event="onchange" rerender="sectionOuter" status="showAltStatus" />
                    </apex:inputCheckbox>      
                </apex:pageBlockSectionItem>       
                <apex:actionStatus startText="(rendering position information...)" id="showAltStatus"/>
            </apex:pageBlockSection>
            <apex:outputPanel layout="block" id="sectionOuter">
                <apex:pageBlockSection rendered="{!showAltPosition}" id="altPositionBlock" title="Select the Alternate Position"  columns="1">
                   <h1>This is a block</h1>
                </apex:pageBlockSection>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

那为什要包一层呢?

实际上是因为,页面加载的时候因为checkbox默认是false的,所以altPositionBlock并没有被加载,也就是HTML页面中没有一个叫altPositionBlock的element,于是JS是无法刷新它的,加一个一直存在的外包装,就OK了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值