复选框控件不一致地计算值

checkbox control not calculating value consistently

我想通过使用 SSJS return "true" 或 "false" 基于会话变量的值来使用复选框控件的功能。页面完全刷新时计算仅需运行s。加载页面后,代码不会进行部分或完全刷新(唯一的例外是代码将在第一次部分或完全刷新时 运行 - 这很有趣)。我写了一个快速演示程序。
我确信这与 JSF 生命周期有关,但我并不擅长破译正在发生的事情的技术。 谢谢, ---丽莎&

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.beforeRenderResponse><![CDATA[#{javascript:if (sessionScope.uncheck === true) {
    sessionScope.checked = false;
} else {
    sessionScope.checked = true;
}}]]></xp:this.beforeRenderResponse>
    <xp:panel id="Panel1">
        <xp:label id="label1" value="Panel1" style="font-weight:bold"></xp:label>
        <xp:button id="button1" value="Button1 - Check the Box">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="partial" refreshId="Panel2">
                <xp:this.action><![CDATA[#{javascript:sessionScope.checked = true;
print("just clicked Button1 sessionScope.checked = ",sessionScope.checked);}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
        <xp:button id="button3" value="Button 2 - Uncheck the Box">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="partial" refreshId="Panel2">
                <xp:this.action><![CDATA[#{javascript:sessionScope.checked = false;
print("just clicked Button2 sessionScope.checked = ",sessionScope.checked);}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
        <xp:button id="button2" value="Reload page - Uncheck the Box">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="complete">
                <xp:this.action><![CDATA[#{javascript:sessionScope.uncheck = true;
context.reloadPage();}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
        <xp:button id="button4" value="Reload page - Check the Box">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="complete">
                <xp:this.action><![CDATA[#{javascript:sessionScope.uncheck = false;
context.reloadPage();}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
    </xp:panel>
    <xp:panel id="Panel2">
        <xp:label id="label2" value="Panel2" style="font-weight:bold">
        </xp:label>
        <xp:checkBox id="checkBox1" text="Computed Checkbox">
            <xp:this.defaultChecked><![CDATA[#{javascript:print("Calculating CheckBox - sessionScope.checked = ",sessionScope.checked);
return sessionScope.checked}]]></xp:this.defaultChecked>
        </xp:checkBox>
        </xp:panel>
    </xp:view>

将您的 sessionScope 变量直接绑定到 xp:checkBox。这样,对 sessionScope 变量的任何更改都会在刷新时反映在复选框中:

<xp:checkBox id="checkBox1" text="Computed Checkbox" value="#{sessionScope.checked}"></xp:checkBox>

下面是一个使用按钮进行部分刷新和完全重新加载的示例:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

    <xp:panel id="Panel1">
        <xp:label id="label1" value="Panel1" style="font-weight:bold"></xp:label>
        <xp:button id="button1" value="Button1 - Check the Box">
            <xp:eventHandler event="onclick" submit="true" refreshMode="partial" refreshId="Panel2">
                <xp:this.action><![CDATA[#{javascript:
                    sessionScope.checked = true;
                    print("just clicked Button1 sessionScope.checked = ",sessionScope.checked);
                }]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
        <xp:button id="button3" value="Button 2 - Uncheck the Box">
            <xp:eventHandler event="onclick" submit="true" refreshMode="partial" refreshId="Panel2">
                <xp:this.action><![CDATA[#{javascript:
                    sessionScope.checked = false;
                    print("just clicked Button2 sessionScope.checked = ",sessionScope.checked);
                }]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>

        <xp:button id="button2" value="Reload page - Uncheck the Box">
            <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
                <xp:this.action><![CDATA[#{javascript:
                    sessionScope.checked = true;
                }]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
        <xp:button id="button4" value="Reload page - Check the Box">
            <xp:eventHandler event="onclick" submit="true" refreshMode="complete">
                <xp:this.action><![CDATA[#{javascript:
                    sessionScope.checked = false;
                }]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
    </xp:panel>

    <xp:panel id="Panel2">
        <xp:label id="label2" value="Panel2" style="font-weight:bold">
        </xp:label>
        <xp:checkBox id="checkBox1" text="Computed Checkbox" value="#{sessionScope.checked}"></xp:checkBox>
    </xp:panel>
</xp:view>