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

    Entities with rich:suggestionBox

    agnadello

      Hello,


      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?


      Cheers!

        • 1. Re: Entities with rich:suggestionBox
          abafna.bafna.amit.gmail.com

          Have you tried



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



          Cheers
          Amit

          • 2. Re: Entities with rich:suggestionBox
            agnadello

            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}" />
                </a:support>
            </rich:suggestionbox>



            In that case it didn't work :-)


            Cheers

            • 3. Re: Entities with rich:suggestionBox
              agnadello

              Sorry!


              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}" />
                  </a:support>
              </rich:suggestionbox>




              Thanks a lot Amit!


              Cheers!

              • 4. Re: Entities with rich:suggestionBox
                niravassar

                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" />
                     </h:panelGrid>
                      <rich:suggestionbox width="250" suggestionAction="#{CityBean.suggestCities}" var="city" for="selectCity" status="none"
                            nothingLabel="No City Name Found">
                          <h:column>
                               <h:outputText value="#{city.name}"/>
                          </h:column>
                          <h:column>
                               <h:outputText value="#{city.id}" />
                          </h:column>
                        <a:support event="onselect">
                             <f:setPropertyActionListener value="#{city}" target="#{city.citySelection}" />
                          </a:support>          
                      </rich:suggestionbox>



                • 5. Re: Entities with rich:suggestionBox
                  bonissauro

                  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.


                  Cheers

                  • 6. Re: Entities with rich:suggestionBox
                    lvdberg

                    Hi,


                    '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:column>
                         <h:outputText value="#{result.fullName}" />
                         </h:column>
                    <a4j:support reRender="serviceTable, growl" eventsQueue="inputQueue"
                     event="onselect" action="#{bean.manager.addInspector(result)}" />
                    </rich:suggestionbox>



                    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.


                    Leo     

                    • 7. Re: Entities with rich:suggestionBox
                      valatharv
                      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:column>
                                      <h:outputText value="#{result}"/>
                              </h:column>
                      </rich:suggestionbox>

                      @Name("nameLookUpBean")
                      @Scope(ScopeType.SESSION)
                      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.

                      Regards