by clicking on one button invoke two other buttons - xpages

I would like by clicking on a button to "activate" the clicking of 2 other buttons.
I've put this ccjs in the onclick event:
document.getElementById("#{id:button28}").click();document.getElementById("#{id:button29}").click();
But then only button 28 gets clicked !
Then I tried putting this part1 of the code under the onclick event and part2 under the onmousedown event. Then I have to click 2 times this button before he actually does the job.
the code so far:
<xp:button value="Save" id="button26">
<xp:eventHandler event="onclick" submit="false">
<xp:this.script>
<xp:executeClientScript>
<xp:this.script><![CDATA[document.getElementById("#{id:button29}").click();
></xp:this.script>
</xp:executeClientScript>
</xp:this.script></xp:eventHandler></xp:button><xp:button id="button29" value="button29">
<xp:eventHandler
event="onclick" submit="true" refreshMode="partial"
id="eventHandler21" refreshId="outsidePanel5">
<xp:this.action>
<xp:actionGroup>
<xp:executeScript>
<xp:this.script><![CDATA[#{javascript:sessionScope.put("test","");// and some more code}]]>
</xp:this.script>
</xp:executeScript>
</xp:actionGroup>
</xp:this.action>
<xp:this.onComplete><![CDATA[document.getElementById("#{id:button28}").click();]]></xp:this.onComplete>
</xp:eventHandler></xp:button><xp:button value="button28" id="button28">
<xp:eventHandler event="onclick" submit="true" refreshMode="partial" refreshId="outsideMogelijkheid">
<xp:this.action>
<xp:executeScript
script="#{javascript://some code}">
</xp:executeScript>
</xp:this.action>
</xp:eventHandler></xp:button>

If you want to run the SSJS in another button, you can actually call the eventHandler programmatically rather than triggering the button in CSJS. This will also perform better, because you're not switching between CSJS and SSJS all the time. The following code should work:
var eventHandler:com.ibm.xsp.component.xp.XspEventHandler = getComponent("button29");
eventHandler.getAction().invoke(facesContext, null);
eventHandler = getComponent("button28");
eventHandler.getAction().invoke(facesContext, null);

If these buttons execute SSJS code you need to add the code to click on the second button in the
onComplete event of the first button.
http://xpagesblog.com/XPagesHome.nsf/Entry.xsp?documentId=0574D334CB03EACF852578CB00667F54
Addition
Working example of onComplete not that it only works with partial refresh
<xp:button value="Save" id="button26">
<xp:eventHandler event="onclick" submit="false">
<xp:this.script><![CDATA[document.getElementById("#{id:button29}").click()]]>
</xp:this.script>
</xp:eventHandler></xp:button>
<xp:button id="button29" value="button29">
<xp:eventHandler event="onclick" submit="true"
refreshMode="partial" refreshId="computedField1">
<xp:this.action><![CDATA[#{javascript:viewScope.test="Almost Done"}]]>
</xp:this.action>
<xp:this.onComplete>
<![CDATA[document.getElementById("#{id:button28}").click();]]>
</xp:this.onComplete>
<xp:this.onError><![CDATA[alert("error")]]></xp:this.onError>
</xp:eventHandler></xp:button>
<xp:button value="button28" id="button28">
<xp:eventHandler event="onclick" submit="true"
refreshMode="partial" refreshId="computedField1">
<xp:this.script><![CDATA[alert("Button 28")]]></xp:this.script>
<xp:this.action>
<xp:executeScript>
<xp:this.script><![CDATA[#{javascript:viewScope.test="Done"}]]>
</xp:this.script>
</xp:executeScript>
</xp:this.action></xp:eventHandler>
</xp:button>
<xp:br></xp:br>
<xp:text escape="true" id="computedField1" value="#{viewScope.test}"></xp:text>

Related

checkbox control not calculating value consistently

I want to use the feature of the checkbox control by using SSJS to return "true" or "false" based on the value of a session variable. The calculation only runs when the page is completely refreshed. The code does not on a partial or full refresh once the page is loaded (The only exception is that the code will run on the first partial or full refresh - which is interesting). I wrote a quick demonstration program.
I am sure this has something to do with the JSF lifecycle, but am not sophisticated in the technology to decipher what is going on.
Thanks,
---Lisa&
<?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>
Bind your sessionScope variable directly to the xp:checkBox. This way any changes to the sessionScope variable will be reflected by the checkbox when refreshed:
<xp:checkBox id="checkBox1" text="Computed Checkbox" value="#{sessionScope.checked}"></xp:checkBox>
Here's an example using your buttons for partial refresh and for full reload:
<?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>

put tabPanel forward: best method?

i tried this with sessionScope (didn't work):
The following Tabs should be controllable with the help of variable 'm1':
<xp:tabbedPanel id="tabbedPanel1"
selectedTab="#{javascript:sessionScope.get('m1')}">
<xp:tabPanel label="Config" id="conf">
<xc:k_conf></xc:k_conf>
</xp:tabPanel>
<xp:tabPanel label="Solution" id="sol">
<xc:k_sol></xc:k_sol>
</xp:tabPanel>
</xp:tabbedPanel>
In Control k_conf is a button 'calculate' which performs an agent and then sets the varable 'm1' to 'sol'.
I would expect, that the Tab switches to 'Solution', but nothing happens (although refreshmode="complete").
<xp:button value="calculate" id="button1">
<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
...
<xp:executeScript>
<xp:this.script><![CDATA[#{javascript:
...
agent.runWithDocumentContext(docVG.getDocument());
sessionScope.put("m1","sol");
}]]>
</xp:this.script>
</xp:executeScript>
...
</xp:button>
Another approach was to use component (didn't work because 'show is not allowed for TabPanel'):
<xp:this.action>
<![CDATA[#{javascript:
var c = getComponent("IdofTab");
c.show("IdofTab");
}]]>
</xp:this.action>
That sessionScope approach should work, I just tried it and verified that. Is there some CSJS part to the button eventHandler that is not returning true and thus preventing the SSJS executeScript from being performed?
For the second approach, you can use the setSelectedTab method of the tabbed panel rather than show, for example:
<xp:button id="button2" value="Change Tab 4">
<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
<xp:this.action>
<![CDATA[#{javascript:
var c = getComponent("tabbedPanel1");
c.setSelectedTab("tabPanel4");
}]]>
</xp:this.action>
</xp:eventHandler>
</xp:button>

Can pagerControls have onclick events?

I've got a dynamic view that totals what's displayed on the screen using a viewScope variable and I want to have it reset my viewScope variable. It doesn't and it appears not to be executing any script I put in there.
<xp:pager partialRefresh="true" id="upperPager"
for="viewRowRepeat" styleClass="tsDynamicViewPager" pageCount="8"
alwaysCalculateLast="true">
<xp:pagerControl id="pagerControl4" type="First">
<xp:eventHandler event="onclick" submit="true" refreshMode="partial" refreshId="viewRowRepeat">
<xp:this.action><![CDATA[#{javascript:dBar.info("pagerControl4 clicked");
clearColumnTotals();}]]></xp:this.action>
<xp:this.script><![CDATA[alert("Clicked!");]]></xp:this.script>
</xp:eventHandler>
</xp:pagerControl>
<xp:pagerControl id="pagerControl5" type="Group">
<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:dBar.info("pagerControl5 clicked");
clearColumnTotals();}]]></xp:this.action>
<xp:this.script><![CDATA[alert("Clicked!");]]></xp:this.script>
</xp:eventHandler>
</xp:pagerControl>
<xp:pagerControl id="pagerControl6" type="Last">
<xp:eventHandler event="onclick"
submit="true" refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:dBar.info("pagerControl6 clicked");
clearColumnTotals();}]]></xp:this.action>
<xp:this.script><![CDATA[alert("Clicked!");]]></xp:this.script>
</xp:eventHandler>
</xp:pagerControl>
</xp:pager>
Needless to say, I don't know if I've coded it incorrrectly or if there isn't supposed to be any onclick event for pagerControls. I know the IDE doesn't show one for pagers themselves.
Connect your pager controls with a Dojo onclick event to execute client side code when pager control is clicked:
<xp:eventHandler
event="onClientLoad"
submit="false">
<xp:this.script><![CDATA[
dojo.query(".xspFirst").forEach(function(node) {
dojo.connect(node, "onclick", function() {
alert("First clicked");
});
});
dojo.query(".xspGroup").forEach(function(node) {
dojo.connect(node, "onclick", function() {
alert("Group clicked");
});
});
...
]]>
</xp:this.script>
</xp:eventHandler>
Classes rendered for pager controls are "xsp" + type like "xspFirst", "xspGroup" and "xspLast". Use this classes to get the controls.

Section close/open with partial refresh

I have a section in my xpage, it's open by default. When i close the section and make a partial refresh it opens the section again.
Is there a easy way the prevent this.
Hehe, according to your problem i build a small XPage to test what i commented here is what i got:
<xp:panel id="refresh">
<xp:section id="section1" initClosed="false">
<xp:panel id="innerRefresh">Section content</xp:panel>
</xp:section>
</xp:panel>
<xp:button value="refresh inner div" id="button3">
<xp:eventHandler event="onclick" submit="true" refreshMode="partial"
refreshId="innerRefresh">
<xp:this.action><![CDATA[#{javascript://refresh}]]></xp:this.action>
</xp:eventHandler>
</xp:button>
<xp:button value="refresh outer div" id="button4">
<xp:eventHandler event="onclick" submit="true" refreshMode="partial"
refreshId="refresh">
<xp:this.action><![CDATA[#{javascript://refresh}]]></xp:this.action>
</xp:eventHandler>
</xp:button>
<xp:button value="refresh section" id="button1">
<xp:eventHandler event="onclick" submit="true" refreshMode="partial"
refreshId="section1">
<xp:this.action><![CDATA[#{javascript://refresh}]]></xp:this.action>
</xp:eventHandler></xp:button>
<xp:button value="full refresh" id="button2">
<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
<xp:this.action><![CDATA[#{javascript://refresh}]]></xp:this.action>
</xp:eventHandler></xp:button>
By trying to refresh the sction in differen ways i saw nothing unusual if i pressed each button once or twice with about one secound in between, but if you press the button "refresh section" multiple times the section opens again. This effekt works in my IE8 and Firefox 19.0.2. I would recomend using a panel/div around your section and refresh it instead of the section direkt.
Update:
Try this, it creates up to 3 sections dynamical and each refreshable by its own button, and they will stay closed,
<xp:this.beforePageLoad><![CDATA[#{javascript:var sectioncount:java.util.Vector = #Explode("1,2,3",",");
viewScope.put("sectioncount",sectioncount);
viewScope.put("selectedSection", sectioncount)
}]]></xp:this.beforePageLoad>
<xp:checkBoxGroup id="checkBoxGroup1"
value="#{viewScope.selectedSection}">
<xp:this.defaultValue><![CDATA[#{javascript:return viewScope.get( "sectioncount" );}]]></xp:this.defaultValue>
<xp:eventHandler event="onchange" submit="true"
refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:// full update //partial update}]]></xp:this.action>
</xp:eventHandler>
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:return viewScope.get( "sectioncount" );}]]></xp:this.value>
</xp:selectItems>
</xp:checkBoxGroup>
<xp:repeat id="repeat2" rows="30" var="varcollection"
repeatControls="true">
<xp:this.value><![CDATA[#{javascript:return viewScope.get( "sectioncount" );}]]></xp:this.value>
<xp:panel>
<xp:this.rendered><![CDATA[#{javascript:var vec:java.util.Vector = viewScope.get( "selectedSection" );
return #IsMember(varcollection,vec);
}]]></xp:this.rendered>
<xp:button value="refresh this section:" id="button3">
<xp:eventHandler event="onclick" submit="true" refreshMode="partial"
refreshId="refresh">
<xp:this.action><![CDATA[#{javascript://refresh}]]></xp:this.action>
</xp:eventHandler>
</xp:button>
<xp:panel id="refresh">
<xp:section id="section1" initClosed="false">
<xp:panel id="innerRefresh">
<xp:text>
<xp:this.value><![CDATA[#{javascript:var date:java.util.Date = new java.util.Date;
return date.toTimeString()}]]></xp:this.value>
</xp:text></xp:panel>
</xp:section>
</xp:panel>
</xp:panel>
</xp:repeat>

XPages How to use an action in an Action Group to stop form submission

I have a button built on an Action Group. I would like to have an action to stop processing the button -- stop the next actions from firing.
I tried a Confirm Action, but the user can click OK and it will continue. I tried an Execute Script that returns false, but that did not do anything.
I have been reading about event handlers, but not sure how to incorporate this in the button's code itself.
Here is the current code behind the button:
<xp:button id="button1" value="Create Account">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete" id="eventHandler1">
<xp:this.action>
<xp:actionGroup>
<xp:modifyField
name="ac_Key" value="#{javascript:#Unique()}"
var="document1">
</xp:modifyField>
<xp:saveDocument></xp:saveDocument>
<xp:openPage
name="/successAccount.xsp">
</xp:openPage>
</xp:actionGroup>
</xp:this.action>
</xp:eventHandler>
</xp:button>
Thanks for all your help!
This simple example uses the confirm action to only continue if the user selects OK. If the user clicks cancel, nothing further happens. If the user clicks OK, the browser opens the index.xsp page.
<xp:button value="Label" id="button1">
<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
<xp:this.action>
<xp:actionGroup>
<xp:confirm message="Sure?"></xp:confirm>
<xp:openPage name="/index.xsp"></xp:openPage>
</xp:actionGroup>
</xp:this.action>
</xp:eventHandler>
</xp:button>
Update: This example uses a condition on the actionGroup that can prevent the actionGroup from executing if the condition is false:
<xp:actionGroup condition="#{javascript:validateSomething()}">
<xp:save name="/opskrifter.xsp"></xp:save>
</xp:actionGroup>

Resources