Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF 2 custom composite component with f:ajax
    text
    copied!<p>I'm trying to create a custom composite component colorPicker using this jQuery plugin <a href="http://www.eyecon.ro/colorpicker/" rel="nofollow">http://www.eyecon.ro/colorpicker/</a>.</p> <p>I´d like to be able to append a jsf tag <code>f:ajax</code>, and when a color is selected, perform an ajax call to the server. I have been testing this functionality and it all appears to be right, but obviously I missed something, because the listener is never called.</p> <p>This is my component code:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:composite="http://java.sun.com/jsf/composite"&gt; &lt;composite:interface&gt; &lt;composite:attribute name="label" /&gt; &lt;composite:clientBehavior name="customEvent" event="change" targets="#{cc.clientId}"/&gt; &lt;/composite:interface&gt; &lt;composite:implementation&gt; &lt;h:outputStylesheet library="css" name="colorpicker/colorpicker.css" /&gt; &lt;h:outputStylesheet library="css" name="colorpicker/layout.css" /&gt; &lt;h:outputScript library="js" name="colorpicker/jquery.js" target="head"/&gt; &lt;h:outputScript library="js" name="colorpicker/colorpicker.js" target="head"/&gt; &lt;h:outputScript library="js" name="colorpicker/eye.js" target="head"/&gt; &lt;h:outputScript library="js" name="colorpicker/utils.js" target="head"/&gt; &lt;h:outputScript library="js" name="colorpicker/layout.js" target="head"/&gt; &lt;h:outputScript library="js" name="colorpicker/hex.js" target="head"/&gt; &lt;div id="#{cc.clientId}" class="colorSelector"&gt; &lt;div style="background-color: #0000FF;"&gt;&lt;/div&gt; &lt;/div&gt; &lt;script&gt; //jQuery(document).ready(function() { jQuery('##{cc.clientId}').ColorPicker({ color: '#0000ff', onShow: function (colpkr) { jQuery(colpkr).fadeIn(2000); alert('onchange1'); launchEvent(document.getElementById('#{cc.clientId}')); alert('onchange2'); //return false; }, onHide: function (colpkr) { jQuery(colpkr).fadeOut(2000); return false; }, onChange: function (hsb, hex, rgb) { } }); //}); /* &lt;![CDATA[ */ function launchEvent(fieldName) { alert('launchEvent1'); if ("fireEvent" in fieldName) { alert('launchEvent2'); fieldName.fireEvent("onchange"); alert('launchEvent3'); } else { alert('launchEvent4'); var evt = document.createEvent("HTMLEvents"); alert('launchEvent5'); evt.initEvent("change", false, true); alert('launchEvent6'); fieldName.dispatchEvent(evt); alert('launchEvent7'); } /* ]]&gt; */ } &lt;/script&gt; &lt;/composite:implementation&gt; &lt;/html&gt; </code></pre> <p>And this is the page implementation:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:r="http://richfaces.org/rich" xmlns:a="http://richfaces.org/a4j" xmlns:s="http://jboss.org/seam/faces" xmlns:cp="http://java.sun.com/jsf/composite/component"&gt; &lt;h:head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;f:view&gt; &lt;h:form prependId="false"&gt; &lt;cp:colorpicker id="colorSelector"&gt; &lt;f:ajax event="customEvent" listener="#{themeBean.changeColor1}" onevent="alert('event raised');"/&gt; &lt;/cp:colorpicker&gt; &lt;h:inputText value="#{themeBean.color1}"&gt;&lt;/h:inputText&gt; &lt;/h:form&gt; &lt;/f:view&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload