How to trigger a click event programmatically?
I'm trying to click panel splitter collaplse/expand button with javascript / jquery, to no avail.
Here is an example of the splitters: http://jdevadf.oracle.com/adf-richclient-demo/faces/components/index.jspx;jsessionid=GTYNMf7Mq2JD6L4v38yCdTh2HLplhJYLTGc1J1TjZFwmpZjcqh1n!-294683649?_afrLoop=28596129526428344&_afrWindowMode=0&_afrWindowId=null#%2Fcomponents%2FpanelSplitter.jspx%40
As you can see, when you click the small buttons with arrows, the regions collapse. If I try to get element and click it, nothing happens.
$("dmoTpl:innerVerticalSplitter::i").onclick()
If I load jquery script and 开发者_Go百科trigger click, also nothing happens. I'm a bit confused how this all works and why script clicks are ignored. Any ideas?
I don't use JQuery, but here is an example that works using vanilla javascript and the <af:clientListener/>
tag.
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:af="http://xmlns.oracle.com/adf/faces/rich" xmlns:f="http://java.sun.com/jsf/core">
<af:resource type="javascript">
function toggleSplitter(evt) {
comp = evt.getSource().findComponent('ps1');
if (comp) {
comp.setProperty("collapsed", !comp.getProperty("collapsed"));
}
else {
alert('not found');
}
}
</af:resource>
<af:panelStretchLayout id="psl1">
<f:facet name="center">
<af:panelSplitter id="ps1">
<f:facet name="first">
<af:outputText value="First" id="ot1"/>
</f:facet>
<f:facet name="second">
<af:panelFormLayout id="pfl1">
<f:facet name="footer"/>
<af:commandButton text="Toggle" id="cb1" immediate="true">
<af:clientListener method="toggleSplitter" type="click"/>
</af:commandButton>
<af:outputText value="Second" id="ot2"/>
</af:panelFormLayout>
</f:facet>
</af:panelSplitter>
<!-- id="af_one_column_stretched" -->
</f:facet>
</af:panelStretchLayout>
</jsp:root>
before click it is expanded:
and after click it is collapsed:
It is click() not onclick() with jQuery, but when I checked your code $ is not jQuery. Are you sure this page has jQuery on it?
Instead of trying to create a click event, if you know the event handler it is typically better to directly call that method directly. Here's an example in jQuery.
Setup of the event handler
var clickEventHandler = function(event) {
...
};
$('#some-button').click(clickEventHandler);
Calling it programmatically later
clickEventHandler();
You can use JQuery to trigger an event on an element. Consider the below code which triggers various events.
$('#myElement').trigger('click');
$('#myElement').trigger('mousedown');
$('#myElement').trigger('touchstart');
For programmatically expand/collapse af:panelBox, create event and queue it programmatically as shown below.
To collapse af:panelBox
DisclosureEvent discEvent = new DisclosureEvent(panelBoxCompObj, false);
discEvent.queue();
To expand af:panelBox
DisclosureEvent discEvent = new DisclosureEvent(panelBoxCompObj, true);
discEvent.queue();
精彩评论