Search code examples
javascripttwitter-bootstrapxpages

Trying to set state of BS accordian in viewscope from CSJS but value is always the same (and not correct)


on my XPage I have a BS accordian defined:

<div class="panel-collapse collapse in" id="collapseOne" aria-expanded="true" style="">
</div>

on an button I would like to save the state of the panel, so after a full refresh I would like to display the accordian panel in it's previous state.

So my button looks now as followed:

<xp:button
        value="Label"
        id="button1">
    <xp:eventHandler
        event="onclick"
        submit="true"
        refreshMode="complete">
        <xp:this.script><![CDATA[if ($('#collapseOne').attr('aria-expanded') == "false") {
"#{javascript:viewScope.put('accordian','collapsed')}";
}else{
"#{javascript:viewScope.put('accordian','expanded')}";
}]]></xp:this.script>
    </xp:eventHandler>
</xp:button>

Whatever state the accordian is the viewScope 'accordian' is set to 'expanded' eventhough the script identifies the correct state of the accordian.

What am I doing wrong?

Update:

I set up as suggested an RPC service but it does not work. what am I doing wrong?

<xe:jsonRpcService
                                id="rpcServiceSetState">
                            <xe:this.methods>
                                <xe:remoteMethod
                                    name="setPanelState"
                                    loaded="true"
                                    script="viewScope.put('accordian', state)">
                                    <xe:this.arguments>
                                        <xe:remoteMethodArg
                                            name="state"
                                            type="string">
                                        </xe:remoteMethodArg>
                                    </xe:this.arguments>
                                </xe:remoteMethod>
                            </xe:this.methods></xe:jsonRpcService>
                            <xp:button id="btnTriggerRPC" value="Trigger RPC Method">
        <xp:eventHandler
            event="onclick"
            submit="true" refreshMode="complete">
            <xp:this.script><![CDATA[if ($('#collapseOne').attr('aria-expanded') == "false") {
    rpcServiceSetState.setPanelState('collapsed')
}else{
    rpcServiceSetState.setPanelState('expanded')
}]]></xp:this.script>
        </xp:eventHandler>
                            </xp:button>

Solution

  • this code below allows me to set a viewscope via RPC service?

    <xe:jsonRpcService id="jsonRpcServiceSelected"
            serviceName="rpcService" rendered="true">
            <xe:this.methods>
                <xe:remoteMethod name="setAccordion"
                    script="viewScope.put('accordion', selected);">
                    <xe:this.arguments>
                        <xe:remoteMethodArg name="selected" type="string" />
                    </xe:this.arguments>
                </xe:remoteMethod>
    
            </xe:this.methods>
        </xe:jsonRpcService>
    <xp:button id="btnTriggerRPC" value="Trigger RPC Method">
            <xp:eventHandler
                event="onclick"
                submit="true"
                refreshMode="complete">
                <xp:this.script><![CDATA[if ($('#collapseOne').attr('aria-expanded') == "false") {
    rpcService.setAccordion('collapsed');
    
    }else{
    
        rpcService.setAccordion('expanded');
    }]]></xp:this.script>
            </xp:eventHandler>
    </xp:button>