7 Replies Latest reply on Jul 7, 2011 3:14 PM by Val Sw

    Entities with rich:suggestionBox

    Andreas Eriksson Newbie


      I've searched the forum, the wiki and the docs but I cannot find any example of using entities with the suggestionbox.

      I have PostalCode objects in my suggestion box which I'd like to set on my input textfield.

      Does someone know how to solve this?


        • 1. Re: Entities with rich:suggestionBox
          Amit Bafna Newbie

          Have you tried

          <a4j:support ajaxSingle="true" event="onselect">
              <f:setPropertyActionListener value="#{entity}" target="#{bean.zipCode}" />


          • 2. Re: Entities with rich:suggestionBox
            Andreas Eriksson Newbie

            Hello Amit,

            Is this what you mean?

            <h:inputText value="#{newAd.postalCode}" id="criteria" />
            <rich:suggestionbox for="criteria" suggestionAction="#{PostalCodeManager.filterByCode}" var="entity" width="350">
                <a:support ajaxSingle="true" event="onselect">
                    <f:setPropertyActionListener value="#{entity}" target="#{entity.postalCode}" />

            In that case it didn't work :-)


            • 3. Re: Entities with rich:suggestionBox
              Andreas Eriksson Newbie


              I guess it did if I removed the value attribute on the inputText:

              <h:inputText id="criteria" />
              <rich:suggestionbox for="criteria" suggestionAction="#{PostalCodeManager.filterByCode}" var="entity" width="350">
                  <a:support ajaxSingle="true" event="onselect">
                      <f:setPropertyActionListener value="#{entity}" target="#{entity.postalCode}" />

              Thanks a lot Amit!


              • 4. Re: Entities with rich:suggestionBox
                Nirav Assar Newbie

                This was a big help. Thanks. 

                This is the version I used.  The example is to select a city with a suggestion box. 

                - the suggestCities method does the filter
                - there is no value for the input text, it is just used as a search
                - the f:setPropertyActionListener uses the selected city and passes it to the bean attribute citySelection.

                     <h:panelGrid columns="1" cellspacing="2" cellpadding="2">
                          <h:outputLabel value="City" />          
                          <h:inputText id="city" size="50" />
                      <rich:suggestionbox width="250" suggestionAction="#{CityBean.suggestCities}" var="city" for="selectCity" status="none"
                            nothingLabel="No City Name Found">
                               <h:outputText value="#{city.name}"/>
                               <h:outputText value="#{city.id}" />
                        <a:support event="onselect">
                             <f:setPropertyActionListener value="#{city}" target="#{city.citySelection}" />

                • 5. Re: Entities with rich:suggestionBox
                  GIOVANNI ROBERTO MACHADO Newbie

                  Allright, it's working fine when you have an already loaded list.

                  But what if I needed an on-line search on the DB ?

                  I tried creating a String attribute on my managedbean e attaching it to the inputtext via Value property, but it didn't seem to work.

                  Could you post the server-side code?

                  10x in advance and sorry about my terrible english.


                  • 6. Re: Entities with rich:suggestionBox
                    Leo van den berg Master


                    've included some code where I look up persons in a DB.

                    <rich:suggestionbox id="suggestionBoxId1" for="text1"  rendered="#{!useTrackingAndTracing}"
                         minChars="2" suggestionAction="#{inspectorCriteria.autocompleteInspector}"
                         var="result" fetchValue="#{result.lastName}" nothingLabel="No inspector found">
                         <h:outputText value="#{result.fullName}" />
                    <a4j:support reRender="serviceTable, growl" eventsQueue="inputQueue"
                     event="onselect" action="#{bean.manager.addInspector(result)}" />

                    And the used method:

                    public List<RoadInspector> autocompleteOfficer(Object suggest) {
                            lastName = (String)suggest;
                            List<RoadInspector> result = roadOfficerQuery.getResultList();
                            return result;

                    It's basically a search on lastnames and it shows the fullname (firs and lastname) in a table.

                    I think this gives you an idea how to do the trick.


                    • 7. Re: Entities with rich:suggestionBox
                      Val Sw Expert
                      Need help for typical scenario....

                      Till now we were simply displaying json array from Restlet web Service in suggestion box which works perfectly fine and comma separated values are persisted against a column in a table. Following is the code used fro suggestion...
                      <h:inputText id="empName" value="#{empHome.instance.empName}"/>
                      <rich:suggestionbox id="suggestionBoxId" for="empName" tokens=","
                              suggestionAction="#{nameLookUpBean.autocomplete}" var="result"
                              fetchValue="#{result}" nothingLabel="no name found" minChars="2">
                                      <h:outputText value="#{result}"/>

                      public class HitHubCompoundLookUpBean {
                              public List<String> autocomplete(Object suggest) throws Exception {      
                                      String pref = (String)suggest;
                                      List<String> result = new ArrayList<String>();       
                                      tmpJSONArray =  getNameFromWebService(pref);//RESTLET WS
                                      for (int i=0;i<tmpJSONArray.length();i++){
                                              result.add((String) tmpJSONArray.get(i));
                                      return result;
                      But we have some typical scenario
                      Situtaion : All suggestion is returned from external web service...
                      a) User enters 2 characters, result will be returned from Restlet web Service in the form of Key-value pair and user can enter more text separated by token, and similarly Key-value pair is returned from web Service.

                      c) We need show only values as suggestion list.

                      b) We need to store comma separated "values" from suggestion box against a column in a table and corresponding "keys" in other column of same table.

                      I am not sure how f:setPropertyActionListener or eventsQueue will work as everytime value will change which is coming from external web service.

                      Please suggest on what can be the correct approach I should follow.