Thursday, March 27, 2014

Execute a client/javascript method on commandlink click in adf?

Sometimes we want to execute a client method which is present in the javascript on a button lick or a commandlink click in oracle ADF. This can be done in two ways

Method #1

Set the attribute <af:clientListener> for the command item as shown below

<af:commandLink text="Click me" id="cl1"
                immediate="true" partialSubmit="true" blocking="true"
    <af:clientListener type="action" method="javascriptMethod"/>
</af:commandLink>


Set the java script as shown below
 
<af:resource type="javascript">
  function disableUserInput(evt) {
      evt.cancel();
      evt.stopPropagation();
  }    

</af:resource> 

Method #2

Create and execute the javascript programmatically in backing bean or managed bean as shown below

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import javax.faces.context.FacesContext;
import org.apache.myfaces.trinidad.util.Service;


String myScript =
    "document.getElementById(clientId).tabIndex = \"-1\";";
ExtendedRenderKitService renderKitService =
    Service.getRenderKitService(FacesContext.getCurrentInstance(),
                                ExtendedRenderKitService.class);
renderKitService.addScript(FacesContext.getCurrentInstance(),
                           myScript);

 

No comments:

Post a Comment