【发布时间】:2015-02-04 18:13:31
【问题描述】:
我遇到了一个奇怪的问题。情况如下:
我正在使用我的解决方案来根据屏幕大小显示和隐藏内容:https://xpagesandme.wordpress.com/2015/01/20/diving-into-bootstrap-and-font-awesome-part-3-displaying-hiding-content-based-on-device-type/
因此,我设置了一个自定义控件,其中包含移动设备的 UI。我正在使用所述自定义控件的“渲染”属性来隐藏或显示它,基于我帖子中的参数值。
这是一个奇怪的问题:
在自定义控件中,我有一个按钮,它应该设置一个作用域变量,在模态框内的面板上进行部分刷新,然后显示模态框(打开模态框的调用在 onComplete 事件中事件处理程序)。
模式打开,但作用域变量未更新。
当我删除自定义控件的呈现属性时,它可以工作。如果我把渲染的属性重新放回去,它就不起作用了。
此外,任何简单的操作(即打开页面)都不起作用。但是,我放入事件处理程序的 onComplete 或 onStart 事件中的 CSJS 将被执行。
有什么想法吗?
P.S.:这是一个 XPage 示例。删除任何按钮的渲染属性,onClick 事件中的代码将起作用:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view
xmlns:xp="http://www.ibm.com/xsp/core">
<xp:eventHandler
event="onClientLoad"
submit="false">
<xp:this.script><![CDATA[$(window).on('load resize', function(){
var screenWidth = $(window).width();
var sDevice = '';
switch(true){
case (screenWidth < 768):
sDevice = 'mobile';
break;
case (screenWidth < 922):
sDevice = 'tablet';
break;
case (screenWidth >= 922):
sDevice = 'desktop'
}
XSP.partialRefreshGet( '#{id:pnlList}', {params: {'sDevice': sDevice}});
});]]></xp:this.script>
</xp:eventHandler>
<xp:panel
id="pnlList">
<xp:button
value="Button Mobile"
id="button1" rendered="#{javascript:param.sDevice == 'mobile';}">
<xp:eventHandler
event="onclick"
submit="true"
refreshMode="partial"
refreshId="pnlToUpdate"
onStart="alert('onStart')"
onComplete="alert('onComplete')">
<xp:this.action><![CDATA[#{javascript:sessionScope.put('sTestValue', 'Button Mobile clicked')}]]></xp:this.action>
</xp:eventHandler></xp:button>
<xp:br></xp:br>
<xp:button
id="button2" value="Button Tablet" rendered="#{javascript:param.sDevice == 'tablet';}">
<xp:eventHandler
event="onclick"
submit="true"
refreshMode="partial"
refreshId="pnlToUpdate"
onStart="alert('onStart')"
onComplete="alert('onComplete')">
<xp:this.action><![CDATA[#{javascript:sessionScope.put('sTestValue', 'Button Tablet clicked')}]]></xp:this.action>
</xp:eventHandler></xp:button>
<xp:br></xp:br>
<xp:button
value="Button Desktop"
id="button3" rendered="#{javascript:param.sDevice == 'desktop';}">
<xp:eventHandler
event="onclick"
submit="true"
refreshMode="partial"
refreshId="pnlToUpdate"
onStart="alert('onStart')"
onComplete="alert('onComplete')">
<xp:this.action><![CDATA[#{javascript:sessionScope.put('sTestValue', 'Button Desktop clicked')}]]></xp:this.action>
</xp:eventHandler></xp:button></xp:panel>
<xp:panel id="pnlToUpdate">
<xp:text
escape="true"
id="computedField1" value="#{sessionScope.sTestValue}">
</xp:text></xp:panel></xp:view>
【问题讨论】:
-
我认为您在部分刷新尝试在其中运行脚本时隐藏了按钮。如果可能,坚持 CSJS 隐藏。
-
非常感谢您的回复 Frantisek。在 Chrome 中,我可以看到,当我单击它时,唯一触发的部分刷新是按钮的部分刷新。此外,按钮事件处理程序的“onStart”和“onComplete”事件会触发(我放入了警报语句)。只是服务器端代码或简单操作没有被执行。
-
我建议您阅读 XPages 中的“JSF 阶段”。 @Knutt 的建议和我的评论是关于当您单击按钮时服务器会更新模型的事实(实际上是 POST 请求,也就是部分刷新)。
标签: javascript xpages