Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label af:clientListener. Show all posts
Showing posts with label af:clientListener. Show all posts

Friday 1 February 2019

Use jQuery Mask to format ADF Input Component

 In this post, I am showing a very interesting and useful jQuery plugin - jQuery Mask Plugin. We can use this plugin to format (mask) input component of ADF form with minimal code

Here I'll show you some examples of masking

Follow these steps to implement this in ADF Application

Create a page in the view controller project of Fusion Web Application and drop an input text in it.

  1. <af:inputText label="Label 1" id="it1" >
  2. </af:inputText>

Now add the jQuery library to the page under the document tag

  1. <af:resource type="javascript" source="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"/>
  2. <af:resource type="javascript" source="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js"/>

Now I am writing a simple jQuery function that masks value of the input field in date format (dd/mm/yyyy)  and add this function to page using resource tag

  1. <af:resource type="javascript">
  2. function maskUserInput() {
  3. $('input[name="it1"]').mask('00/00/0000');
  4. }
  5. </af:resource>

Add a client listener to inputText to call jQuery function as soon as the user starts typing

  1. <af:inputText label="Label 1" id="it1" >
  2. <af:clientListener method="maskUserInput" type="keyDown"/>
  3. </af:inputText>

Now run and enter some value in the input field and with typing, it'll mask user input


Now try changing the format as per your requirement, Suppose I need to mask the phone number in (+XX)-XXXXXXXXXX format, so for that, I have used this function.

  1. <af:resource type="javascript">
  2. function maskUserInput() {
  3. $('input[name="it1"]').mask('(+00)-0000000000');
  4. }
  5. </af:resource>

and output is


IP Address

  1. <af:resource type="javascript">
  2. function maskUserInput() {
  3. $('input[name="it1"]').mask('099.099.099.099');
  4. }
  5. </af:resource>

Output is


You can always change the jQuery function as per your requirement.

Cheers 🙂 Happy Learning

Monday 17 September 2018

Set current date in af:inputDate on double click using javascript in ADF

This post is about a question that is asked on the OTN forum. In this post, I'll show you how we can set the current date in af:inputDate component with a double click of the mouse. For this, we need to use a simple javascript function.



Here we have an inputDate component on the page and added javascript function as a resource in the page. See page XML source

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
  3. xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  4. <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  5. <f:view>
  6. <af:document title="SetCurrentDate.jspx" id="d1">
  7. <af:resource type="javascript">
  8. function setDate(evt) {
  9. var comp = evt.getSource()
  10. comp.setValue(new Date());
  11. }
  12. </af:resource>
  13. <af:form id="f1">
  14. <af:inputDate label="Label 1" id="id1">
  15. <af:clientListener method="setDate" type="dblClick"/>
  16. </af:inputDate>
  17. </af:form>
  18. </af:document>
  19. </f:view>
  20. </jsp:root>

All done, as you can see that the javascript function is called using clientListener on double click event of input date component.


Cheers ðŸ™‚ Happy Learning

Thursday 4 June 2015

Create and set clientAttribute to ADF Faces component programmatically to pass value on client side JavaScript


This post is next in series of "Working with ADF Faces Components programmatically"

So this post is about creating client Attribute, applying it to component and setting it's value programmatically
this requirement comes in picture when user is dealing with dynamic layout means components are created programmatically at run time and it is not possible to apply clientAttribute and other properties declarative

In this i am extending my previous post -
Apply Client/Server listener to programmatically created components, apply JavaScript to ADF Faces components at run time

In previous post i have described about applying client listener and server listener programmatically
here we will see how to pass a variable value to java script function using client attribute
You can read more about af:clientAttribute here

From oracle docs-
The clientAttribute tag specifies the name/value for an attribute which will both be made available both on the server-side (Faces) component as well on on the client-side equivalent. This tag will be ignored for any server-rendered components, as it is only supported for the rich client. ClientAttributes are not synchronized to the server since unknown attributes from the client are not synchronized to the server.



Lets' see how we can do this ,It's simple just check this code -

//Code from previous post to create inputText programmatically 
//http://www.awasthiashish.com/2015/05/apply-clientserver-listener-to.html
        RichInputText ui = new RichInputText();
        ui.setValue("Programmatically Created Input Text");
        ui.setId("pit1");
        ui.setContentStyle("width:200px;color:navy");

Here i am not writing code to apply client/server listener again , refer previous post for that
See the code to add client attributes to component (inputText)

        // A Set that contains all clientAttributes
        java.util.Set<String> clientAttributes = new HashSet<String>();

        // Add client attribute's name
        clientAttributes.add("clientAttribute1");
        clientAttributes.add("clientAttribute2");
        clientAttributes.add("clientAttribute3");

        //Add these attributes to a UI Compoenent
        ui.setClientAttributes(clientAttributes);

        //here assign values to client attributes
        ui.getAttributes().put("clientAttribute1", "Oracle ADF");
        ui.getAttributes().put("clientAttribute2", 9999);
        ui.getAttributes().put("clientAttribute3", "Ashish Awasthi");

So all done , now check it
For that i have used this javascript method

function demoJsFunction(evt) {
                  var comp = evt.getSource();
                  alert(comp.getProperty('clientAttribute1'));
                  alert(comp.getProperty('clientAttribute2'));
                  alert(comp.getProperty('clientAttribute3'));
               
                  evt.cancel();
              }
 

this method is called using client listener on input text and see the alert messages
First client attribute value-

Second client attribute value-

Third client attribute value-

So this is working :)
Cheers :) Happy Learning

Friday 29 May 2015

Apply Client/Server listener to programmatically created components, apply JavaScript to ADF Faces components at run time

Hello All

This post is next in series of "Working with ADF Faces Components programmatically"
Previous posts are-

Creating dynamic layout (form and UI Component) using ADF Faces
Get Value from programmatically created components , Iterate over parent component to get child values in ADF Faces 
Apply ActionListener to programmatically created buttons/link in ADF

Now this post is about applying client listener & server listener (to execute some javascript function) to a programmatically created component (inputText, outputText etc)

Let's see the implementation part (Jdev 12.13) -

  • First created a FusionWebApplication and a page in viewController project
  • Dropped a button on page , on this button action i will create an inputText programmatically
  • To create new inputText i have added following code (described in previous posts)



  •     /**Method to add dynamically created component to a parent layout
         * @param parentUIComponent
         * @param childUIComponent
         */
        public void addComponent(UIComponent parentUIComponent, UIComponent childUIComponent) {
            parentUIComponent.getChildren().add(childUIComponent);
            AdfFacesContext.getCurrentInstance().addPartialTarget(parentUIComponent);
        }
    


            //Code to create af:inputText and add it to panelgroup layout
            RichInputText ui = new RichInputText();
            ui.setValue("Programmatically Created Input Text");
            ui.setId("pit1");
            ui.setContentStyle("width:200px;color:navy");
            //getParentGroupLayoutBind is the component binding of parent panel group layout
            addComponent(getParentGroupLayoutBind(), ui);
    

  • Now first part is done - Creation of input text , next is to assign client and server listener to it
    So for that first we have to add some JavaScript function in page that will be called using clientListener
    I have added this JavaScript function in page, it just fires a custom event from client and pass component value as parameter

  •         function demoJsFunction(evt){
              var comp = evt.getSource();
              alert(comp);
              AdfCustomEvent.queue(comp, "ServerEventToGetValue", {fvalue:comp.getSubmittedValue()}, true);
              evt.cancel();
            }
          
    

  • One more thing is to define server listener method in managed bean so that we can apply it to inputText at run time

  •     /**Server Listener - It will be called on execution of client side javascript
         * @param clientEvent
         */
        public void testServerListener(ClientEvent clientEvent) {
            //To get value passed from Client Event
            String val = clientEvent.getParameters().get("fvalue").toString();
            FacesMessage msg=new FacesMessage("Server Listener Called and value in inputText is - " + val);
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            FacesContext.getCurrentInstance().addMessage(null, msg);
           
        }
    

  • Now create clientListener/ServerListener programmatically and assign both to inputText
    Pass server listener method name along with managed bean name

  •         // Create new Client Listener and assign method name and type
            ClientListenerSet CL = new ClientListenerSet();
            CL.addListener("click", "demoJsFunction");
            //Add Server listener , assign client event name and resolve pre-defined serverlistener
            CL.addCustomServerListener("ServerEventToGetValue",
                                       getMethodExpression("#{viewScope.Testbean.testServerListener}"));
    
            // Add clientListener/ServeListener to inputText
            ui.setClientComponent(true);
            ui.setClientListeners(CL);
    

    Helper method to resolve expression-

        /**To Resolve ServerListener method
         * @param s
         * @return
         */
        public MethodExpression getMethodExpression(String s) {
    
            FacesContext fc = FacesContext.getCurrentInstance();
            ELContext elctx = fc.getELContext();
            ExpressionFactory elFactory = fc.getApplication().getExpressionFactory();
            MethodExpression methodExpr = elFactory.createMethodExpression(elctx, s, null, new Class[] {
                                                                           ClientEvent.class });
            return methodExpr;
    
        }
    

  • All done , now run application and check , click on button it will create a inputText
         now click on input text and see server listener is called :)

This is how we can apply javascript in programmatic created components
Cheers :) Happy Learning

Wednesday 24 December 2014

Update page component in between of event processing in ADF Faces using JavaScript

This post is not about any specific topic of framework, recently i was working in an application, requirement was like this
There is a button on page that uploads a large file to server and it takes some time . So as soon as user press this button it's text should be changed to 'Processing..' and after upload is complete it should be 'Done'




In this post i am sharing same - How to update page component while an event is processing ?
First i have tried it using java means in same button action listener but in this case button text was changed only after event processing is complete
So for this purpose i have used some javascript function, this is quite simple (see the implementation)

  • Drop a button on page and create a ActionListener , it will change button text to 'Done' after event processing (used Thread.sleep() for some long processing)

  •     private RichButton buttonBind;
    
        /**Method to process Action Event
         * @param actionEvent
         */
        public void processValueAction(ActionEvent actionEvent) {
            // Sleep for some time (Event Processing time)
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //After processing set button text to 'Done'
            buttonBind.setText("Done");
            AdfFacesContext.getCurrentInstance().addPartialTarget(buttonBind);
        }
    
        public void setButtonBind(RichButton buttonBind) {
            this.buttonBind = buttonBind;
        }
    
        public RichButton getButtonBind() {
            return buttonBind;
        }
    

  • Now question is how to change button text to 'Processing..' immediately after clicking button. So for this purpose i am using javascript

  • function changeButtonText(event) {
                      var comp = event.getSource();
                      comp.setText('Processing..');
                  }
    

  • Calling this javascript function using af:clientListener in button like this (See XML source of page)

  •  <af:document title="UpdatePageComponent.jspx" id="d1">
                <af:resource type="javascript">
                  function changeButtonText(event) {
                      var comp = event.getSource();
                      comp.setText('Processing..');
                  }
                </af:resource>
                <af:form id="f1">
                    <af:spacer width="10" height="10" id="s1"/>
                    <af:panelGroupLayout id="pgl1" layout="horizontal" halign="center">
                        <af:panelBox id="pb1" showDisclosure="false">
                            <f:facet name="toolbar"/>
                            <af:button text="Click to process" id="b1"
                                       actionListener="#{viewScope.UpdatePageComponent.processValueAction}"
                                       clientComponent="true" binding="#{viewScope.UpdatePageComponent.buttonBind}"
                                       inlineStyle="width:250px;text-align:center;padding:5px;font-weight:bold;">
                                <af:clientListener method="changeButtonText" type="action"/>
                            </af:button>
                        </af:panelBox>
                    </af:panelGroupLayout>
                </af:form>
            </af:document>
    

  • As soon as button is clicked this javascript function will be invoked and set new text in button and after event processing button text is again set by managed bean actionListener.


  • After Clicking button (Event is processing)-

    Processing complete-

Thanks, Happy Learning :)