2 Replies Latest reply on Nov 24, 2007 5:14 PM by Siobhan Ernest

    Populate more than one input with suggestionBox onselect

    Siobhan Ernest Newbie

      Hi -

      I am wondering if there is a way to populate more than one input field using the suggestion box? For example - I can find a Student by name, but I also want to populate the form with the student id:

       <h:form id="suggestionbox_form">
       <h:panelGrid columns="2" rowClasses="prop" columnClasses="name,value">
       <h:outputLabel for="studentName">Student Name</h:outputLabel>
       <h:inputText value="NAME GOES HERE" id="studentName"/>
       <h:outputLabel for="studentId">Student Id</h:outputLabel>
       <h:inputText value="ID GOES HERE" id="studentId"/>
       <rich:suggestionbox for="studentName"
       suggestionAction="#{quickFindService.quickFindStudentByName}"
       var="suggest" minChars="3" fetchValue="#{suggest.studentName}"
       width="150" height="100">
       <h:column>
       <h:outputText value="#{suggest.studentName}"/>
       </h:column>
       <h:column>
       <h:outputText value="#{suggest.studentId}"/>
       </h:column>
       </rich:suggestionbox>
      
       </h:panelGrid>
       </h:form>


      I can easily populate the studentName field - that works - but I would also like to set the studentId field onselect, and subsequently submit the form to the student page with the selected Id. Autocompletiing on the studentId would work fine, but the client needs to search by name, and there are too many Smiths to count on a unique student result.

      I have been looking for an example of what I want, but having no luck. Any help is appreciated.

        • 2. Re: Populate more than one input with suggestionBox onselect
          Siobhan Ernest Newbie

          Hi again -

          Thanks for the tip Ilya, but I am still stuck. Form looks like this now:

          <h:form id="suggestionbox_form">
           <h:panelGrid columns="2" rowClasses="prop" columnClasses="name,value">
           <a4j:keepAlive beanName="quickFindService.student" ajaxOnly="true"/>
           <a4j:region renderRegionOnly="false">
           <h:outputLabel for="studentName">Student Name</h:outputLabel>
           <h:inputText value="#{quickFindService.student.studentName}" id="studentName"/>
           <h:outputLabel for="studentId">Student Id</h:outputLabel>
           <h:inputText value="#{quickFindService.student.studentId}" id="studentId"/>
          
           <rich:suggestionbox id="studentSuggestionBox" for="studentName"
           suggestionAction="#{quickFindService.quickFindStudentByName}"
           var="suggest" first="0" minChars="3" fetchValue="#{suggest.studentName}"
           width="200" height="100" ajaxSingle="true">
           <h:column>
           <h:outputText value="#{suggest.studentName}"/>
           </h:column>
           <h:column>
           <h:outputText value="#{suggest.studentId}"/>
           </h:column>
           <a4j:support event="onselect" action="#{quickFindService.onSelect}" reRender="studentId"
           ajaxSingle="true">
           <f:setPropertyActionListener value="#{suggest.studentId}"
           target="#{quickFindService.student.studentId}"/>
           </a4j:support>
           </rich:suggestionbox>
           </a4j:region>
           </h:panelGrid>
           <a4j:log popup="false" level="WARN" width="800" height="100"></a4j:log>
           <a4j:outputPanel ajaxRendered="true">
           <h:messages/>
           </a4j:outputPanel>
           </h:form>


          and the backing bean:

          @Stateful
          @Name("quickFindService")
          @Scope(ScopeType.CONVERSATION)
          public class QuickFindAction implements QuickFind, Serializable {
          
           @Logger
           Log log;
          
           @In
           EntityManager entityManager;
          
           private Object event;
           private Student student ;
          
           public List<Student> quickFindStudentByName(Object event) {
           List<Student> results = new ArrayList<Student>();
           if (event != null) {
           String pref = event.toString();
           results = entityManager.createQuery("select student from Student student where lower(student.studentName) like concat(lower(:pref),'%')")
           .setParameter("pref", pref)
           .getResultList();
           }
           return results;
           }
          
          
           public void selectStudent() {
          
           }
          
           public void onSelect() {
           log.info("Student: " + student.getStudentName() + "; id: " + student.getStudentId() );
          
           }
          
           public Object getEvent() {
           return event;
           }
          
           public void setEvent(Object event) {
           this.event = event;
           }
          
           public Student getStudent() {
           if(student == null) {
           student = new Student() ;
           }
           return student;
           }
          
           public void setStudent(Student student) {
           this.student = student;
           }
          
           @Remove
           @Destroy
           public void destroy() {
          
           }
          }


          Still no joy. The value in #suggest.studentId is always null in the setPropertyActionListener, and so the studentId field never gets set correctly. I can set it to any string literal I want, or a #messages property, but not the #suggest value I need. The #suggest.studentId in the suggestion dataTable show correctly, so I know my query is pulling back the student properly.

          There is something I am not doing right here, and I've been staring at it so long, I'm afraid I am missing something terribly obvious.

          Help appreciated.