4 Replies Latest reply on Oct 7, 2009 11:55 AM by Val Sw

    Issue with suggestion Box Capital example, autocomplete alwa

    Val Sw Expert

      Hi,

      I am not able to catch why autocomplete() bean method is called twice in capitals sample. Please let me know what I am doing wrong...

      -- When I input anything (e.g. w) in suggestionBox, CapitalsBean.autocomplete() function is called, which calls "getCapitals().iterator()" for the value passed say "w" : OK
      -- Issue is :It calls CapitalsBean.autocomplete() which calls "getCapitals().iterator()" again on the same event with pref and suggest as null.

      I tried providing the condition checking "pref" as well as "suggest" for not null before calling "getCapitals().iterator()"

      Log:
      CapitalsBean entered >>>>>>>>>>>
      CapitalsBean entered >>>>>>>>>>>
      CapitalsBean.autocomplete(), pref passed is >>>> w
      CapitalsBean.autocomplete(), suggest passed is >>>> w
      CapitalsBean.autocomplete(), value passed is NOT NULL getting Capitals >>>>
      In CapitalsBean.getCapitals() result BEFORE return is : 50

      CapitalsBean.autocomplete(), pref passed is >>>>
      CapitalsBean.autocomplete(), suggest passed is >>>>
      CapitalsBean.autocomplete(), value passed is NOT NULL getting Capitals >>>>
      In CapitalsBean.getCapitals() result BEFORE return is : 50


      CapitalsBean
      @Name("capitalsBean")
      public class CapitalsBean {
       private ArrayList<Capital> capitals = new ArrayList<Capital>();
       private ArrayList<String> capitalsNames = new ArrayList<String>();
       private List<SelectItem> capitalsOptions = new ArrayList<SelectItem>();
       private String capital = "";
      
       private String currentNameFilterValue;
      
       public List<Capital> autocomplete(Object suggest) {
       String pref = (String)suggest;
       ArrayList<Capital> result = new ArrayList<Capital>();
       System.out.println("CapitalsBean.autocomplete(), pref passed is >>>> "+pref);
       System.out.println("CapitalsBean.autocomplete(), suggest passed is >>>> "+suggest);
      
       if(<checked all the conditions for "pref" and "suggest" for not null>){
       System.out.println("CapitalsBean.autocomplete(), value passed is NOT NULL getting Capitals >>>> ");
       Iterator<Capital> iterator = getCapitals().iterator();
       while (iterator.hasNext()) {
       Capital elem = ((Capital) iterator.next());
       if ((elem.getName() != null && elem.getName().toLowerCase().indexOf(pref.toLowerCase()) == 0) || "".equals(pref))
       {
       result.add(elem);
       }
       }
       }else{
       System.out.println("CapitalsBean.autocomplete(), value is NULL DO NOT get Capitals >>>> ");
       }
      
       return result;
       }
      
       public CapitalsBean() {
       System.out.println("CapitalsBean entered >>>>>>>>>>> ");
       URL rulesUrl = getClass().getResource("capitals-rules.xml");
       ...
       capitalsNames.clear();
       for (Capital cap : capitals) {
       capitalsNames.add(cap.getName());
       }
       capitalsOptions.clear();
       for (Capital cap : capitals) {
       capitalsOptions.add(new SelectItem(cap.getName(),cap.getState()));
       }
       }
      
       public ArrayList<Capital> getCapitals() {
       System.out.println("In CapitalsBean.getCapitals() result BEFORE return is : "+capitals.size());
       return capitals;
       }


      XHTML:
      <h:inputText value="#{capitalsBean.capital}" id="text" />
       <rich:suggestionbox id="suggestionBoxId" for="text" tokens=","
       suggestionAction="#{capitalsBean.autocomplete}" var="result"
       fetchValue="#{result.name}"
       nothingLabel="No capitals found" columnClasses="center"
       usingSuggestObjects="true"
       minChars="1">
       <h:column>
       <h:outputText value="#{result.name}" />
       </h:column>
       </rich:suggestionbox>