7 Replies Latest reply on Dec 19, 2009 7:54 AM by idyoshin

    rich:suggestionbox

    jpalmer1026.jpalmer1026.mchsi.com

      I'm trying to use a rich:suggestionbox to allow the user to type in a couple of letters of an employee's last name and then be able to select an employee from the corresponding list of matches. This is working fine. The problem I'm having is in persisting the selected user to the database? My code is as follows:




      <s:decorate id="employeeField" template="layout/edit.xhtml">               
           <ui:define name="label">Employee</ui:define>          
           <h:inputText id="employee" value="#{dmaCaseHome.instance.employee.fullName}">
             <rich:suggestionbox id="suggestionBox" for="employee" 
                  fetchValue="#{dmaCaseHome.instance.employee}"
                  suggestionAction="#{employeeList.autoComplete}" 
                  var="employee" minChars="2">
                       <h:column>
                         <h:outputText value="#{employee.fullName}" />
                    </h:column>
                 </rich:suggestionbox>
              <s:convertEntity />
           </h:inputText>
      </s:decorate>



      Anyone see what I'm doing wrong?


        • 1. Re: rich:suggestionbox
          germanescobar

          Post the stacktrace to see the problem.

          • 2. Re: rich:suggestionbox
            jpalmer1026.jpalmer1026.mchsi.com

            I think the real problem that I'm facing is how to convert the selected value to an entity so that it can be persisted to the database. Can this be done using an h:inputText component?

            • 3. Re: rich:suggestionbox
              valatharv

              Please elaborate on the issue while persisting... is there any exception, is the value null, or is it using id of the entity instead of value, or etc.

              • 4. Re: rich:suggestionbox
                jpalmer1026.jpalmer1026.mchsi.com

                I'm getting a java.lang.NumberFormatException: For the input string. I'm assuming this is because the String isn't being converted into an Employee object before getting persisted. I'm not sure why though since I'm using the s:convertyEntity tag?

                • 5. Re: rich:suggestionbox
                  germanescobar

                  Yes, that's not the way of doing that. Try this:




                  <s:decorate id="employeeField" template="layout/edit.xhtml">               
                       <ui:define name="label">Employee</ui:define>          
                       <h:inputText id="employee" value="#{dmaCaseHome.instance.employee.fullName}">
                         <rich:suggestionbox id="suggestionBox" for="employee" 
                              fetchValue="#{dmaCaseHome.instance.employee}"
                              suggestionAction="#{employeeList.autoComplete}" 
                              var="employee" minChars="2">
                                   <h:column>
                                     <h:outputText value="#{employee.fullName}" />
                                </h:column>
                                      <a4j:support event="onselect">
                                           <f:setPropertyActionListener value="#{employee}" target="#{dmaCaseHome.instance.employee}" />
                                    </a4j:support>
                  
                  
                             </rich:suggestionbox>
                          
                       </h:inputText>
                  </s:decorate>



                  It should be something like that. I just added the a4j:support part and removed the s:convertEntity. You can also take a look at this document. Hope it helps!

                  • 6. Re: rich:suggestionbox
                    jpalmer1026.jpalmer1026.mchsi.com

                    I thought it would be easier to do something like the following, but this only displays a list of primary keys instead of a list of names like I'd like:




                    <s:decorate id="employeeField" template="layout/edit.xhtml">               
                         <ui:define name="label">Employee</ui:define>          
                         <rich:comboBox id="employee" value="#{dmaCaseHome.instance.employee}">
                              <s:selectItems var="_employee" value="#{employees}" label="#{_employee.lastName}"/>     
                           <s:convertEntity />
                         </rich:comboBox>
                    </s:decorate>



                    Am I doing something wrong?

                    • 7. Re: rich:suggestionbox
                      idyoshin

                      Hello Jeff,


                      I guess I understand what you're trying to do.


                      First of all inputText in scenario with suggestionbox is just a helper. So you shouldn't use it's  value binding.


                      The second one you should add setter on selection in the suggestionbox. And because your page doesn't have the direct value binding to the edited property you should add some manual required validation (in case it's needed).



                      Have a look at the following code:


                      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                           xmlns:h="http://java.sun.com/jsf/html"
                           xmlns:f="http://java.sun.com/jsf/core"
                           xmlns:ui="http://java.sun.com/jsf/facelets"
                           xmlns:rich="http://richfaces.org/rich"
                           xmlns:a4j="http://richfaces.org/a4j"
                           xmlns:s="http://jboss.com/products/seam/taglib"
                           xmlns:c="http://java.sun.com/jstl/core">
                      
                           <h:panelGrid>
                                <h:panelGroup>
                                     <h:inputText id="suggest_input"  />
                                     <a4j:commandButton value="Clear"
                                          reRender="__descriptionInfo" immediate="true" id="_clearButton">
                                          <f:setPropertyActionListener target="#{value}" value="#{null}"  />
                                     </a4j:commandButton>
                                     <s:link view="/crud/spr/contragents/spr_fiz_lic/edit.xhtml" target="contragentEdit"  
                                               value="Створити нового" immediate="true" bypassUpdates="true" propagation="none"/>
                                </h:panelGroup>
                                <rich:suggestionbox for="suggest_input" minChars="3" width="500"
                                     var="_fiz_lico_suggest"
                                     fetchValue="#{_fiz_lico_suggest.description}"
                                     nothingLabel="нічого не знайдено"
                                     suggestionAction="#{sprFizLicSearch.autoComplete}"
                                     disableDefault="true" ignoreDupResponses="true" immediate="true"
                                     reRender="__descriptionInfo">
                                     <h:column>
                                               <h:panelGroup>
                                                    <h:panelGrid columns="2">
                                                         <h:outputText value="ПІБ"/> <b><h:outputText value="#{_fiz_lico_suggest.description}" /></b>
                                                         <h:outputText value="Дата народження"/> <b><h:outputText value="#{_fiz_lico_suggest.denRojd}"><s:convertDateTime pattern="dd.MM.yyyy"/></h:outputText></b>
                                                         <h:outputText value="Ідентифікаційний код"/> <b><h:outputText value="#{_fiz_lico_suggest.identifCod}"/></b>
                                                    </h:panelGrid>
                                               </h:panelGroup>
                                     </h:column>
                                     <a4j:queue name="q1" />
                                     <a4j:support event="onselect"
                                          reRender="__descriptionInfo" eventsQueue="q1" immediate="true">
                                          <f:setPropertyActionListener value="#{_fiz_lico_suggest}" target="#{value}" />
                                     </a4j:support>
                                </rich:suggestionbox>
                      
                                <a4j:outputPanel id="__descriptionInfo" ajaxRendered="true">
                      
                                     <ui:decorate template="/crud/spr/contragents/spr_fiz_lic/output.xhtml">
                                          <ui:param name="value" value="#{value}" />
                                     </ui:decorate>
                                </a4j:outputPanel>
                      
                           </h:panelGrid>
                      </ui:composition>




                      here I defined some facelet's composition for using in many pages :)


                      the _clearButton sets value to zero.


                      And the main part is in the following ajaxified support:


                      <a4j:support event="onselect" reRender="__descriptionInfo" eventsQueue="q1" immediate="true">
                           <f:setPropertyActionListener value="#{_fiz_lico_suggest}" target="#{value}" />
                      </a4j:support/>




                      And don't forget to use immediate="true" in your suggestionbox scenario - because if you have some validation problem on the other fields in form - your actions wouldn't be executed.



                      Best regards,


                      Ilya Dyoshin