5 Replies Latest reply on Apr 13, 2011 6:11 PM by ujjets

    How to progrmmatically add  ajax support to a UIComponent

    ujjets Newbie

      Hi All,

       

      I have a requirement where I have to programmatically create UI Components like text fields, drop down etc.

      I can do that, but am wondering if I can add ajax support to the components as well.

      Basically I need to do the below programmatically :

      <h:inputText value="#{bean.text}">

            <a4j:support event="onkeyup" reRender="output" action="#{bean.action}"/>

      </h:inputText>

       

      HtmlInputText htmlInputText = (HtmlInputText)    getApplication().createComponent(HtmlInputText.COMPONENT_TYPE);

      ?? add ajax support

       

      Inputs much appreciated.

       

      Thanks.

        • 1. How to progrmmatically add  ajax support to a UIComponent
          Nick Belaevski Master

          Hi,

           

          Check the Facelet handler code for examples: org.ajax4jsf.taglib.html.facelets.AjaxSupportHandler

          1 of 1 people found this helpful
          • 2. How to progrmmatically add  ajax support to a UIComponent
            ujjets Newbie

            Hi Nick,

             

            Thanks for your response.

            I did try looking up the api related to org.ajax4jsf.taglib.html.jsp but was unable to get ideas upt now so I am still looking.

            I am kind of new to this.I found a link http://community.jboss.org/message/558921 that has given me some guidance.

            If possible please point me to some code samples.

            Thanks again.

            • 3. How to progrmmatically add  ajax support to a UIComponent
              Ilya Shaikovsky Master

                  private UIPanel panel = null;

               

               

                  public void setPanel(UIPanel panel) {

                      this.panel = panel;

                  }

                 

                  public UIPanel getPanel() {

                      return panel;

                  }

               

               

                  public void createPanel() {

                      FacesContext fc = FacesContext.getCurrentInstance();

                      Application app = fc.getApplication();

                      UIInput input = (UIInput)app.createComponent("javax.faces.Input");

                      input.setId("input");

                      UIAjaxSupport ajaxSupport = (UIAjaxSupport)app.createComponent("org.ajax4jsf.Support");

                      ajaxSupport.setEvent("onkeyup");

                      input.getFacets().put("ajaxSupport", ajaxSupport);

                      panel.getChildren().add(input);

                  }

              and

              <h:form id="myform">

                                  <h:panelGrid columns="2" id="grid">

                                            <rich:panel binding="#{userBean.panel}"/>

                                  </h:panelGrid>

                                  <a4j:commandButton ajaxSingle="true" action="#{userBean.createPanel}" reRender="grid" value="Add Input"></a4j:commandButton>

                        </h:form>

              works fine for me.

              1 of 1 people found this helpful
              • 4. How to progrmmatically add  ajax support to a UIComponent
                ujjets Newbie

                Hi Ilya,

                 

                Thank you very much for your inputs.I will try it and update the thread.

                • 5. How to progrmmatically add  ajax support to a UIComponent
                  ujjets Newbie

                  Hi Ilya,

                   

                  I tried your code and it works.

                  *******************************************

                  public static UIComponent createUIAjaxSupport(String event,String action,String reRender){

                    UIAjaxSupport ajaxSupport = (UIAjaxSupport)getApplication().createComponent(UIAjaxSupport.COMPONENT_TYPE);

                       ajaxSupport.setEvent(event);

                       MethodExpression methodExpression =

                        getApplication().getExpressionFactory().createMethodExpression(getELContext(),

                          action,

                                null,

                                new Class[] { ActionEvent.class });

                       ajaxSupport.setActionExpression(methodExpression);

                       if (StringUtils.isNotEmpty(reRender)){

                        ajaxSupport.setReRender(reRender);

                       }

                       return ajaxSupport;

                  }

                   

                  then

                   

                  htmlInputText.getFacets().put("ajaxSupport", createUIAjaxSupport("onkeyup","#{bean.someMethod}",null));

                   

                  *******************************************

                  Once again thanks for your valuable answer and hoping this will help others.

                   

                  Regards,

                   

                  Ujjwal