简体   繁体   中英

how to define a conditional xp:confirm?

I would like to use the xp:confirm action on a conditional status. Is there a way to compute the rendered property of this control?

I can compute the rendered property for the whole xp:eventHandler but not for an xp:actionGroup or xp:confirm.

xp:confirm just adds client-side JavaScript to the button with:

if (!XSP.confirm("YOUR MESSAGE")){
    return false;
}

XSP.confirm() is an XPages wrapper for JavaScript confirm. The most flexible approach is to code the client-side JavaScript you want on the "Client" tab of the button, doing return false to prevent the button sending to the server as required.

This is a very synthetic snippet which you however can fit to your needs, if I got you right and if it suits your use case. Not so elegant, but works.

<xp:panel id="mainPanel">
    <xp:button id="myBtn1"
        value="Ask confirmation on submit">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="partial" refreshId="mainPanel">
            <xp:this.action>
                #{javascript:
                    requestScope.put('myAnyVar', 'askConfirmation');
                }
            </xp:this.action>
        </xp:eventHandler>
    </xp:button>
    <xp:button id="myBtn2"
        value="Dont ask confirmation on submit">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="partial" refreshId="mainPanel">
            <xp:this.action>
                #{javascript:
                    requestScope.put('myAnyVar','dontAskConfirmation');
                }
            </xp:this.action>
        </xp:eventHandler>
    </xp:button>
    <input type="hidden" id="doConfirm"
        value="#{requestScope.myAnyVar eq 'askConfirmation' ? '1' : '0'}">
    </input>
</xp:panel>
<br />
<br />
<xp:button id="btnSubmit"
    value="Submit">
    <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete">
        <xp:this.script>
            var confirmOption = dojo.byId('doConfirm').value;
            if (confirmOption === '1') {
                if (confirm('Proceed the submit?')) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        </xp:this.script>
        <xp:this.action>
            #{javascript:
                viewScope.smthSubmitted = new java.util.Date();
            }
        </xp:this.action>
    </xp:eventHandler>
</xp:button>
<br />
<br />
<xp:text value="Submit time: #{viewScope.smthSubmitted}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM