0
votes

I'm setting value of input field on click event of the check box. The editable field is used to validate group of check boxes. This code works in one xpage. When I tried to replicate this code in other xpage, it is not working.

Here is a working code:

    <xp:checkBox
        text="pH"
        id="checkBox1"
        value="#{document1.PH}"
        checkedValue="pH">
        <xp:eventHandler
        event="onclick"
        submit="true"
        refreshMode="partial"
        refreshId="routineSectionInput1"
        execMode="partial">
            <xp:this.action>
                <xp:executeScript>
                    <xp:this.script><![CDATA[#{javascript:var  
checkBox1:com.ibm.xsp.component.xp.XspInputCheckbox = getComponent("checkBox1");
    var routineSectionInput1:com.ibm.xsp.component.xp.XspInputText = getComponent("routineSectionInput1");
    if (checkBox1.getValue()=='pH'){
    routineSectionInput1.setValue('Selected');
    } else {
    routineSectionInput1.setValue('');
    }}]]></xp:this.script>
                                               </xp:executeScript>
            </xp:this.action>
            </xp:eventHandler>
            </xp:checkBox>

I used same field names and same code on another xpage and it is not working. What I'm doing wrong?

Best regards

3

3 Answers

5
votes

When you work with component logic where binding is involved, don't go after the component, go after the data that defines their value. So your code would rather look like this:

    <xp:checkBox text="pH" id="checkBox1"
    value="#{document1.PH}" checkedValue="pH">
    <xp:eventHandler
    event="onclick"
    submit="true"
    refreshMode="partial"
    refreshId="routineSectionInput1"
    execMode="partial">
        <xp:this.action>
            <xp:executeScript>
                <xp:this.script><![CDATA[#{javascript:var chkValue = document1.getItemValueString("PH");
                viewScope.routineSection = (chkValue=="pH") ? "Selected" : ""; 
            }]]></xp:this.script>
            </xp:executeScript>
        </xp:this.action>
        </xp:eventHandler>
        </xp:checkBox>
        <xp:text id="routineSectionInput1" value="#{viewScope.routineSection}"></xp:text>

Hope that helps

1
votes

If there are validation errors or components have data that is of the wrong data type (e.g. text that cannot be parsed as a number in a component bound to a Number field, so conversion errors), any SSJS will fail.

I'd recommend adding a print statement at the start of the SSJS to check whether it's firing. (If you're more confident with XPages, use a PhaseListener to check the correct phase is being triggered).

Also, it's worth adding an Display Errors control to catch any validation errors caught during the partial refresh, ensuring it's within the refresh area (otherwise the errors will not be displayed in the browser).

"Process data without validation" option may be of use here, it you want to skip validation. Note that conversion errors will still prevent SSJS running.

As Stephan says, use the datasource rather than the component, if possible. The datasource will have been updated before the SSJS fires.

0
votes

Removed the checkboxes, compacted the database with -c and added back those checkboxes with the same code. It is working now.