7 Replies Latest reply on Sep 22, 2009 11:53 AM by nimo22

    How to access the current Value of a component using rich:co

    shino

      Hi there,
      I have a form with multiple h:inputText components inside, now I want to implement a quick search feature in such a way that there is a search button next to every inputText. The way things should work is that the user can either complete the form manually or fill one of the fields and click on the search icon. The input on that field should than be used as search criteria.

      Here is a quick draft on what the original form looks like

      <h:form>
      <h:inputText id="INPUT_NAME" value="#{bean.name}" />
      <h:inputText id="INPUT_STREET" value="#{bean.street}"/>
      <h:inputText id="INPUT_PHONE" value="#{bean.phone}"/>
      <h:commandButton action="bean.submit" />
      </form>
      


      and here is what I think might be a solution (though I can't get it to work)
      <h:form>
      <h:inputText id="INPUT_NAME" value="#{bean.name}" />
      <a4j:commandLink
      action="#{searchBean.submit}"
      immediate="true"
      oncomplete="Richfaces.showModalPanel('#{searchBean.resultModalID}');"
      reRender="#{searchBean.modalID}">
      <f:setPropertyActionListener
      value="#rich:clientId('INPUT_NAME')}.component.value"
      target="#{searchBean.searchInput}" />
      </a4j:commandLink>
      
      <h:inputText id="INPUT_STREET" value="#{bean.street}"/>
      <a4j:commandLink
      action="#{searchBean.submit}"
      immediate="true"
      oncomplete="Richfaces.showModalPanel('#{searchBean.resultModalID}');"
      reRender="#{searchBean.modalID}">
      <f:setPropertyActionListener
      value="#rich:clientId('INPUT_STREET')}.component.value"
      target="#{searchBean.searchInput}" />
      </a4j:commandLink>
      
      <h:inputText id="INPUT_PHONE" value="#{bean.phone}"/>
      <a4j:commandLink
      action="#{searchBean.submit}"
      immediate="true"
      oncomplete="Richfaces.showModalPanel('#{searchBean.resultModalID}');"
      reRender="#{searchBean.modalID}">
      <f:setPropertyActionListener
      value="#rich:clientId('INPUT_PHONE')}.component.value"
      target="#{searchBean.searchInput}" />
      </a4j:commandLink>
      <h:commandButton action="bean.submit" />
      </form>
      


      I think I'm doing something wrong with the rich:clientID-Stuff, I'd be great if someone could give me some advice as to what I'm doing wrong.

      Thanks in advance!

      chris

      P.S.: I might have missed it, but is there a list of functions that I can call using the rich:component('id') shortcut? I know there is for example rich:component('id').show() but what else can I do here?

        • 1. Re: How to access the current Value of a component using ric
          ilya_shaikovsky

          rich:component is a shortcut which encoded as document.getElementById('clientId').component. designed in order to have quick access to RichFaces client side objects. And standard h:inputText has no such object.

          you could use #{rich:element('id')}.value which is equals to document.getElementById('clientId').value

          • 2. Re: How to access the current Value of a component using ric
            nimo22

            Can I do something like that?

            <h:outputText id="test1" value="#{rich:element('test2')}.value"/>
            <h:outputText id="test2" value="33" />


            So I want to get the value from "test2" into "test1" without using a4j:support.

            But this "document.getElementById('formDataTableUsers:testt').value" is rendered instead of "33".

            • 3. Re: How to access the current Value of a component using ric
              shino

              Hi Ilya, thanks for your reply, this is the code I use:

              <h:form>
              <h:inputText id="INPUT_NAME" value="#{bean.name}" />
              
              <f:setPropertyActionListener
              value="#{rich:element('INPUT_NAME')}.value"
              target="#{commonAddressQuickSearchFormBacking.searchInput}"/>
              
              </h:form>
              


              Unfortunately, the value I is written to commonAddressQuickSearchFormBacking.searchInput is not the currently typed in string but:
              "document.getElementById('INPUT_NAME').value"

              I'll try to figure out what is going on but as always, help is appreciated ;-)

              • 4. Re: How to access the current Value of a component using ric
                ilya_shaikovsky

                sorry, did not get the point from the beggining. in general al the functions element, clientId and component rendered as JS calls so you can't use them in this way.

                And actually I'm not sure why you need the parameters at all? Why just not to remove immediate from link and use bean values submitted from inputs?

                • 5. Re: How to access the current Value of a component using ric
                  shino

                  Ah I forgot to add some important details to the form I posted here (I thought posting a simplified version would be easier to read but of course it doesn't help much if it doesn't cover all important detials =P).

                  The reason for the immediate attribute is, that some of the inputText-components in the form have required="true" so without immediate="true" filling just one inputText and pressing the search button will cause validation errors..

                  • 6. Re: How to access the current Value of a component using ric
                    nbelaevski

                     

                    "nimo22" wrote:
                    Can I do something like that?

                    <h:outputText id="test1" value="#{rich:element('test2')}.value"/>
                    <h:outputText id="test2" value="33" />


                    So I want to get the value from "test2" into "test1" without using a4j:support.

                    But this "document.getElementById('formDataTableUsers:testt').value" is rendered instead of "33".

                    Use
                    #{rich:findComponent('test2').value}
                    .

                    Note: while findComponent() is useful for quick prototyping or concept testing, I won't recommend to use it in production applications because:
                    - it makes pages complex to maintain
                    - it has low performance.

                    You can use "binding" attribute as an alternative or extract component instance from FacesEvent subclasses.

                    • 7. Re: How to access the current Value of a component using ric
                      nimo22

                      hello,

                      #{rich:findComponent('test2').value}


                      cool trick, thank you! This is really handy!

                      thanks!